How to put parentheses or brackets in regular expressions in Javascript?

Asked

Viewed 962 times

2

If I do something like:

var word='carro';
var re = RegExp( '\\b'+word+'\\b','g' );

It works perfectly, but what follows:

var word='[';
var re = RegExp( '\\b'+word+'\\b','g' );

Returns:

Invalid regular Expression: / b[ b/: Unterminated Character class

How can I pass parentheses or brackets to regular expression without errors?

1 answer

11


Just escape the character with \. Just remembering that inside a string, the character itself \ should be written as \\:

let word = '\\[';
let re = RegExp('\\b' + word + '\\b', 'g');
console.log(re);  // imprime "/\b\[\b/g"

This happens because parentheses and brackets are characters that have special meanings in regex. If you want them to "lose their powers" and be interpreted as the characters themselves, you should escape them with \.

In the case of square brackets, they define a character class. For example, [abc] means "the letter a, or the letter b, or the letter c" (only one of them). If a regex has a [, must have the ] correspondent.

When you didn’t do the escape, the result was a [ without the corresponding closure (the character ]) and this gives error, since it is a badly formed expression.

Using the escape, it is interpreted as the character itself [, no special meaning (and so need not have the ] correspondent), and there works.


Parentheses are the same:

let word = '\\(';
let re = RegExp('\\b' + word + '\\b', 'g');
console.log(re);  // imprime "/\b\(\b/g"

Similarly to square brackets, the parentheses have their function in regex: serve to group a sub-expression and also form a catch group. Therefore a ( must have the ) correspondent.

But using the escape, it happens to be interpreted as a common character, and therefore does not need the respective closure.


The escape works for any other character that has special meaning in regex, such as point (.), meaning "any character", the +, meaning "one or more occurrences", among others (all cited in the documentation).

One detail to note is that not always the expression without escape will give error (as happened with the brackets and parentheses), but will eventually generate a different regex. Ex:

// usando o "+" sem escape
let word = 'a+b';
let re = RegExp('\\b' + word + '\\b', 'g');
console.log(re);  // imprime "/\ba+b\b/g"

// usando o "+" com escape
word = 'a\\+b';
re = RegExp('\\b' + word + '\\b', 'g');
console.log(re);  // imprime "/\ba\+b\b/g"

The first regex is /\ba+b\b/g. In case we have a+b, which means "one or more letters a, followed by a letter b" (example of this regex working).

The second regex is /\ba\+b\b/g. As now the + is escaped, it does not mean "one or more occurrences" and is interpreted only as the character itself +. Hence the excerpt a\+b means "the letter a, followed by the character +, followed by the letter b" (example of this regex working).


Notice I used let instead of var to declare variables. If you didn’t know and were curious, read here to understand the difference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.