Escape regexp

This commit is contained in:
Angelos Chalaris
2017-12-10 16:55:32 +02:00
parent 542a08cf2a
commit 2fa6e7bba8
2 changed files with 20 additions and 0 deletions

View File

@ -16,6 +16,7 @@
* [Curry](#curry)
* [Difference between arrays](#difference-between-arrays)
* [Distance between two points](#distance-between-two-points)
* [Escape regular expression](#escape-regular-expression)
* [Even or odd number](#even-or-odd-number)
* [Factorial](#factorial)
* [Fibonacci array generator](#fibonacci-array-generator)
@ -120,6 +121,16 @@ var distance = x0, y0, x1, y1 =>
Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2))
```
### Escape regular expression
Use `replace()` to escape special characters.
```js
escapeRegExp = s =>
s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
```
### Even or odd number
Use `Math.abs()` to extend logic to negative numbers, check using the modulo (`%`) operator.

View File

@ -0,0 +1,9 @@
### Escape regular expression
Use `replace()` to escape special characters.
```js
escapeRegExp = s =>
s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
```