Daniel Brockman
Daniel Brockman

Reputation: 19290

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)?

Upvotes: 7

Views: 1622

Answers (1)

Daniel Brockman
Daniel Brockman

Reputation: 19290

npm install regexp-quote

RegExp.quote = require('regexp-quote') // optional
new RegExp(RegExp.quote(foo))

Upvotes: 17

Related Questions