JavaScript and regular expressions: literal syntax Vs. RegExp object

前端 未结 2 1267
野趣味
野趣味 2021-02-07 05:16

I have some troubles with this small JavaScript code:

var text=\"Z Test Yeah ! Z\";

// With literal syntax, it returns tr         


        
相关标签:
2条回答
  • 2021-02-07 05:50

    You need to double escape \ characters in string literals, which is why the regex literal is typically preferred.

    Try:

    'Z[\\s\\S]*?Z'
    
    0 讨论(0)
  • 2021-02-07 05:55

    I think it's because you have to escape your backslashes, even when using single quotes. Try this:

    new RegExp('Z[\\s\\S]*?Z','g')
    
    0 讨论(0)
提交回复
热议问题