I\'m looking for an easy way to turn this string:
(java || javascript) && vbscript
Into this string:
(str.search(\'java
Just a fixed version with correct capture and using 1 as backtrack index. See details in "Specifying a string as a parameter" section of String.replace.
mystring.replace(/([-\w]+)/g, "str.search('$1')");
You can call replace
:
mystring.replace(/[-\w]+/g, "str.search('$&')");
Note that this is an XSS hole, since the user input can contain '
s.