JavaScript - escape special characters in regular expression pattern
In this short article, we would like to show how to escape special characters from patterns that we want to use with regular expressions in JavaScript.
Practical example:
xxxxxxxxxx
const SPECIAL_CHARACTERS_EXPRESSION = /[.*+?^${}()\[\]\\|]/g;
// Escapes regular expression pattern special characters in the indicated string.
//
const escapePattern = (pattern) => {
return pattern.replace(SPECIAL_CHARACTERS_EXPRESSION, '\\$&');
};
// Usage example:
const pattern = escapePattern('.*');
const expression = new RegExp(pattern, 'g');
const text = 'this is example text with .* and .* and .*';
const result = text.match(expression);
console.log(pattern); // '\.\*'
console.log(result); // ['.*', '.*', '.*']
Hint: the above approach is useful when we want to escape all special characters automatically.
We can escape special characters manually by using \
or \\
before them in patterns.
In strings, we should escape special characters using \\
.
xxxxxxxxxx
const expression = new RegExp('\\.\\*', 'g');
const text = 'this is example text with .* and .* and .*';
const result = text.match(expression);
console.log(result); // ['.*', '.*', '.*']
In syntax patterns, we should escape special characters using \
.
xxxxxxxxxx
const expression = /\.\*/g;
const text = 'this is example text with .* and .* and .*';
const result = text.match(expression);
console.log(result); // ['.*', '.*', '.*']
Regular expressions are slower than source code, so using some tricks we are able to get optimal performance on pattern escape.
escapePattern()
function is faster by about 2.5x than the replace-based version.
xxxxxxxxxx
// Escapes regular expression pattern special characters in the indicated string.
//
const escapePattern = (pattern) => {
let result = '';
for (let i = 0; i < pattern.length; ++i) {
const entry = pattern[i];
switch (entry) {
case '.':
case '*':
case '+':
case '?':
case '^':
case '$':
case '{':
case '}':
case '(':
case ')':
case '[':
case ']':
case '|':
case '\\':
result += '\\' + entry;
break;
default:
result += entry;
break;
}
}
return result;
};
// Usage example:
const pattern = escapePattern('.*');
const expression = new RegExp(pattern, 'g');
const text = 'this is example text with .* and .* and .*';
const result = text.match(expression);
console.log(pattern); // '\.\*'
console.log(result); // ['.*', '.*', '.*']
escapePattern()
function is faster by about 1.5x than the replace-based version.
xxxxxxxxxx
const SPECIAL_CHARACTERS = {'.': 1, '*': 1, '+': 1, '?': 1, '^': 1, '$': 1, '{': 1, '}': 1, '(': 1, ')': 1, '[': 1, ']': 1, '|': 1, '\\': 1};
// Escapes regular expression pattern special characters in the indicated string.
//
const escapePattern = (pattern) => {
let result = '';
for (let i = 0; i < pattern.length; ++i) {
const entry = pattern[i];
result += (entry in SPECIAL_CHARACTERS ? '\\' + entry : entry);
}
return result;
};
// Usage example:
const pattern = escapePattern('.*');
const expression = new RegExp(pattern, 'g');
const text = 'this is example text with .* and .* and .*';
const result = text.match(expression);
console.log(pattern); // '\.\*'
console.log(result); // ['.*', '.*', '.*']