Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to regexp-quote a string in Node.js?

I want to create a regexp that matches exactly the string foo, even if foo contains metacharacters. This is sometimes known as RegExp.quote. Is there a way to do this in a Node.js environment (or, more broadly, v8/Chrome/WebKit/JavaScript)?

like image 930
Daniel Brockman Avatar asked Apr 29 '12 22:04

Daniel Brockman


1 Answers

npm install regexp-quote

RegExp.quote = require('regexp-quote') // optional
new RegExp(RegExp.quote(foo))
like image 81
Daniel Brockman Avatar answered Oct 21 '22 04:10

Daniel Brockman