Replace any words with modified version of themselves

前端 未结 2 1495
广开言路
广开言路 2021-01-25 13:38

I\'m looking for an easy way to turn this string:

(java || javascript) && vbscript

Into this string:

(str.search(\'java         


        
相关标签:
2条回答
  • 2021-01-25 13:51

    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')");
    
    0 讨论(0)
  • 2021-01-25 13:59

    You can call replace:

    mystring.replace(/[-\w]+/g, "str.search('$&')");
    

    Note that this is an XSS hole, since the user input can contain 's.

    0 讨论(0)
提交回复
热议问题