The scenario: Users type a keyword(e.g. hello) in my asp.net mvc application and then click search, in the C# code I build a JavaScript RegExp string(/hello/i), which will be passed into a query of MongoDB, the whole query looks like:
db.Posts.find( { "title" : /hello/i } )
to get all posts with hello in its title.
When the keyword contains special characters (like \ or (), the js regexp is built incorrectly.
Is there any C# library to parse it?
You’re looking for
Regex.Escape.Although Javascript and .Net use different regex engines, I believe that the .Net escaper is good enough for a JS regex.