Is There a Regexp.Escape Function in JavaScript

Is there a RegExp.escape function in JavaScript?

The function linked in another answer is insufficient. It fails to escape ^ or $ (start and end of string), or -, which in a character group is used for ranges.

Use this function:

function escapeRegex(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

While it may seem unnecessary at first glance, escaping - (as well as ^) makes the function suitable for escaping characters to be inserted into a character class as well as the body of the regex.

Escaping / makes the function suitable for escaping characters to be used in a JavaScript regex literal for later evaluation.

As there is no downside to escaping either of them, it makes sense to escape to cover wider use cases.

And yes, it is a disappointing failing that this is not part of standard JavaScript.

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))

Does regex syntax provide a way of escaping a whole string, rather than escaping characters one by one?

It appears that regex does not offer such a syntax, at least for Javascript. It is possible, however, to proceed as follows:

  1. use a string and automatically escape all the special characters in it,
    as indicated here: Javascript regular expression - string to RegEx object
  2. concatenate that string with strings representing the rest of the expression you want to create
  3. transform the string into a regex expression as indicated in Escape string for use in Javascript regex .

How to properly escape characters in regexp

\Q...\E doesn't work in JavaScript (at least, they don't escape anything...) as you can see:

var s = "*";
print(s.search(/\Q*\E/));
print(s.search(/\*/));

produces:

-1
0

as you can see on Ideone.

The following chars need to be escaped:

  • (
  • )
  • [
  • {
  • *
  • +
  • .
  • $
  • ^
  • \
  • |
  • ?

So, something like this would do:

function quote(regex) {
return regex.replace(/([()[{*+.$^\\|?])/g, '\\$1');
}

No, ] and } don't need to be escaped: they have no special meaning, only their opening counter parts.

Note that when using a literal regex, /.../, you also need to escape the / char. However, / is not a regex meta character: when using it in a RegExp object, it doesn't need an escape.

Why does new RegExp() string not require escaping a forward slash?

Forward slashes are only special characters in that they designate the syntactical beginning and end of a literal regular expression in Javascript. But when you use the constructor, that's not needed, because the first parameter provided to new RegExp is the (entire) string to construct the regular expression from.

Node.js Why does RegExp() throw error with \?

Many characters in regular expressions have special meaning. A backslash \ is the escape character in regular expressions, and needs another character to follow it. If you just want to search for a fixed string where RegExp semantics is ignored, you need to escape the special characters.



Related Topics



Leave a reply



Submit