EN
TypeScript - escape special characters in regular expression pattern
3 points
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 TypeScript.
Practical example:
xxxxxxxxxx
1
const SPECIAL_CHARACTERS_EXPRESSION: RegExp = /[.*+?^${}()\[\]\\|]/g;
2
3
// Escapes regular expression pattern special characters in the indicated string.
4
//
5
const escapePattern = (pattern: string): string => {
6
return pattern.replace(SPECIAL_CHARACTERS_EXPRESSION, '\\$&');
7
};
8
9
10
// Usage example:
11
12
const pattern: string = escapePattern('.*');
13
const expression: RegExp = new RegExp(pattern, 'g');
14
15
const text: string = 'this is example text with .* and .* and .*';
16
const result: string[] = text.match(expression);
17
18
console.log(pattern); // '\.\*'
19
console.log(result); // ['.*', '.*', '.*']
Output:
xxxxxxxxxx
1
\.\*
2
[ '.*', '.*', '.*' ]
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
1
const expression: RegExp = new RegExp('\\.\\*', 'g');
2
3
const text: string = 'this is example text with .* and .* and .*';
4
const result: string[] = text.match(expression);
5
6
console.log(result); // ['.*', '.*', '.*']
Output:
xxxxxxxxxx
1
['.*', '.*', '.*']
In syntax patterns, we should escape special characters using \
.
xxxxxxxxxx
1
const expression: RegExp = /\.\*/g;
2
3
const text: string = 'this is example text with .* and .* and .*';
4
const result: string[] = text.match(expression);
5
6
console.log(result); // ['.*', '.*', '.*']
Output:
xxxxxxxxxx
1
[ '.*', '.*', '.*' ]
escapePattern()
function is faster by about 2.5x than the replace-based version.
xxxxxxxxxx
1
// Escapes regular expression pattern special characters in the indicated string.
2
//
3
const escapePattern = (pattern: string): string => {
4
let result = '';
5
for (let i = 0; i < pattern.length; ++i) {
6
const entry = pattern[i];
7
switch (entry) {
8
case '.':
9
case '*':
10
case '+':
11
case '?':
12
case '^':
13
case '$':
14
case '{':
15
case '}':
16
case '(':
17
case ')':
18
case '[':
19
case ']':
20
case '|':
21
case '\\':
22
result += '\\' + entry;
23
break;
24
25
default:
26
result += entry;
27
break;
28
}
29
}
30
return result;
31
};
32
33
34
// Usage example:
35
36
const pattern: string = escapePattern('.*');
37
const expression: RegExp = new RegExp(pattern, 'g');
38
39
const text: string = 'this is example text with .* and .* and .*';
40
const result: string[] = text.match(expression);
41
42
console.log(pattern); // '\.\*'
43
console.log(result); // ['.*', '.*', '.*']
Output:
xxxxxxxxxx
1
\.\*
2
[ '.*', '.*', '.*' ]
escapePattern()
function is faster by about 1.5x than the replace-based version.
xxxxxxxxxx
1
const SPECIAL_CHARACTERS: Record<string, number> = {'.': 1, '*': 1, '+': 1, '?': 1, '^': 1, '$': 1, '{': 1, '}': 1, '(': 1, ')': 1, '[': 1, ']': 1, '|': 1, '\\': 1};
2
3
// Escapes regular expression pattern special characters in the indicated string.
4
//
5
const escapePattern = (pattern: string): string => {
6
let result = '';
7
for (let i = 0; i < pattern.length; ++i) {
8
const entry = pattern[i];
9
result += SPECIAL_CHARACTERS[entry] ? '\\' + entry : entry;
10
}
11
return result;
12
};
13
14
15
// Usage example:
16
17
const pattern: string = escapePattern('.*');
18
const expression: RegExp = new RegExp(pattern, 'g');
19
20
const text: string = 'this is example text with .* and .* and .*';
21
const result: string[] = text.match(expression);
22
23
console.log(pattern); // '\.\*'
24
console.log(result); // ['.*', '.*', '.*']
Output:
xxxxxxxxxx
1
\.\*
2
[ '.*', '.*', '.*' ]