MyTetra Share
Делитесь знаниями!
How to escape regular expression special characters using javascript?
Время создания: 13.07.2018 15:30
Текстовые метки: javascript regexp escape special characters
Раздел: Javascript

Use the \ character to escape a character that has special meaning inside a regular expression.


To automate it, you could use this:


function escapeRegExp(text) {

return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');

}

Update: There is now a proposal to standardize this method, possibly in ES2016: https://github.com/benjamingr/RegExp.escape


Update: The abovementioned proposal was rejected, so keep implementing this yourself if you need it.

Так же в этом разделе:
 
MyTetra Share v.0.53
Яндекс индекс цитирования