What is the difference between JS regular expression literals and regular expressions created using the new regexp constructor

Time:2021-10-27

When we only use character replacement in JS, we often encounter two writing methods, direct literal

1、str.replace(/ /ig,””);

2. New regexp

Test code

<form name=form1> 
String: < input name = "T1" value = "123456" > 
Mode: / < input name = "T2" value = "^ \ d * $" >/ 
</form> 
<script language=javascript> 
function c1() 
 { 
 re=new RegExp("^\d*$"); 
 alert(re.test("123456")); 
 } 
function c2(form) 
 { 
 re=new RegExp(form.t2.value); 
 alert(re.test(form.t1.value)); 
} 
function c3(){ 
 re=/^\d*$/; 
 alert(re.test("123456")); 
 } 
 c1(); 
 c2(document.form1); 
 c3(); 
</script>

The above code results are: false, true, true

Why is the first result false?

The first should be   re=new   RegExp(“^\\d*$”);     

\Need escape in quotation marks

The first pair has double quotation marks. Double quotation marks need to be escaped one more time, and the second one doesn’t. That’s the difference

However, it seems that in an article today, the replacement of complex classes is still written with new. The premise also has to optimize the regularization.

Many articles have said that literal is more efficient than new object form, but it seems that this is not the case here.
However, I can’t directly deny this view, because I always use literal, concise and beautiful, and convenience is the king.

I think it is necessary to use new regexp when there is a large amount of data or repeated operations.
Because you’ve seen so much performance improvement.
Of course, the precondition is that your regularization must be optimized. If regularization is not optimized, the two are similar.

So optimizing your regexp and then using new regexp can greatly improve the performance of your program.

PS: ie11 is a special case. It never plays cards in the same way.

Well, today’s sharing is over. You’re all ready to move. Go and create new.

What is the difference between JS regular expression literals and regular expressions created with regexp constructors?

The following is an excerpt from a book
Using regular expression literals is not the same as using regexp constructors to create regular expressions. In ECMAScript 3, regular expression literals always share the same regexp instance, and each new regexp instance created using the constructor is a new instance. Let’s look at the following example.


var re = null,
  i;
for (i=0; i < 10; i++){
 re = /cat/g;
 re.test("catastrophe");
}
for (i=0; i < 10; i++){
 re = new RegExp("cat", "g");
 re.test("catastrophe");
}

In the first loop, even if it is specified in the loop body, only one regexp instance is actually created for / cat /. Since the instance property will not be reset, calling the test () method again in the loop will fail. This is because the first call to test () found “cat”, but the second call started with the character with index 3 (the end of the last match), so it could not be found. Since it will test to the end of the string, the next call to test () will start from the beginning again.
The second loop uses the regexp constructor to create a regular expression in each loop. Because each iteration creates a new regexp instance, each call to test () returns true.

I didn’t understand. I tested the next two methods and returned true 10 times
If we let go of the Es5 standard, if it is Es3, does it mean that the first case is 5 times and the second case is 10 times?


var re = null,
 b = 0,
 c = 0,
 tmp,
 i;
for (i=0; i < 10; i++){
 re = /cat/g;
 tmp = re.test("catastrophe");
 console.log(tmp);
 if(tmp){
  b++ ;
 }
}
for (i=0; i < 10; i++){
 re = new RegExp("cat", "g");
 tmp = re.test("catastrophe");
 console.log(tmp);
 if(tmp){
  c++;
 }
}
console.log(b,c);

You said it was the standard of Es3. Now Es5 is popular, and the emergence of ES6 is just around the corner.

Either way, an independent regular expression will be created in the Es5 standard. Modern browsers generally support Es5.

Because now we have to look at ECMA 5.1, which is clearly pointed out in the standard

A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the literal is evaluated. Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals’ contents are identical.

Regular expression literals are converted into a regular object every time they are calculated. Even if the contents are the same, these objects are not the same

Recommended Today

Operations that change non-concurrent collections must have exclusive access.

Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection’s state is no longer correct.   Calling the background API prompts 500 error, and the interface returns the following information. as a result of: The dictionary is modified by multiple threads at […]