Javascript new RegExp vs. /pattern/ and multi line

后端 未结 1 1045
自闭症患者
自闭症患者 2021-01-20 23:23

Why is the /pattern/ matching, but the RegExp not?

##content##

works!

##/content##
1条回答
  •  醉话见心
    2021-01-21 00:05

    Problem is this line:

    var r = new RegExp("##content##([\S\s]*?)##\/content##", "img");
    

    It should be replaced with:

    var r = new RegExp("##content##([\\S\\s]*?)##\/content##", "img");
    

    Reason: Understand that RegExp object takes a String as argument to construct and you need to double escape \S and \s to be correctly interpreted by RegEx engine so \S should become \\S and \s should become \\s in your regex.

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