Why Do Regex Constructors Need to Be Double Escaped

Why do regex constructors need to be double escaped?

You are constructing the regular expression by passing a string to the RegExp constructor.

\ is an escape character in string literals.

The \ is consumed by the string literal parsing…

const foo = "foo";

const string = '(\s|^)' + foo;

console.log(string);

JavaScript RegExp : Why causes a double backslash ( \\ ) an error?

I believe the reason you are getting this error is that the effective regex which you are feeding into the JavaScript engine is a single backslash \.

The reason for this is that the first backslash escapes the second one. So you are putting in a literal backslash, which doesn't make any sense.

RegExp constructor escapes slash character but not dot

An unescaped / denotes the beginning and end of a regular expression. When you pass in a string containing / into the constructor, of course that / is part of the regular expression, not a symbol denoting the beginning or end.

The . is something else entirely, and has nothing to RE delimiters, so it's left as-is.

Note that if you want the regular expression to match a literal dot (rather than any character), you need to double-escape it when using the constructor:

console.log(new RegExp('\\.git'));

Why does the regex stop working when constructed as a string?

When you use the constructed new RegExp () you do not need to escape or enclosing (//) the string. Your second regex may looks like this:

var regex = new RegExp('^(?:533-)?\\d{3}-\\d{4}$');
console.log('533-123-4567'.match(regex));

Refer to the documentation here.

However, the first regex need yo be escape because you are not calling the constructor and you have to precise to the javascript that you are writing is some regex.

Not able to escape ? but \ works fine in javascript regex

Passing the literal notation instead of the string notation to the constructor your regexp works:

var reg = new RegExp(/aa\?b/, 'g');

var msgg = "aa?b"

console.log(msgg.match(reg))

Why have two '\' in Regex?

why have two '\' before 's' and 't'?

In regex the \ is an escape which tells regex that a special character follows. Because you are using it in a string literal you need to escape the \ with \.

and what's this "[\s\t\xa0\u3000]" mean?

It means to match one of the following characters:

  • \s white space.
  • \t tab character.
  • \xa0 non breaking space.
  • \u3000 wide space.

This function is inefficient because each time it is called it is converting a string to a regex and then it is compiling that regex. It would be more efficient to use a Regex literal not a string and compile the regex outside the function like the following:

var trimRegex = /(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g;

function trim(str) {
return String(str).replace(trimRegex, "");
}

Further to this \s will match any whitespace which includes tabs, the wide space and the non breaking space so you could simplify the regex to the following:

var trimRegex = /(^\s+)|(\s+$)/g;

Browsers now implement a trim function so you can use this and use a polyfill for older browsers. See this Answer



Related Topics



Leave a reply



Submit