Escaping quotes in a javascript string

前端 未结 1 1349
旧时难觅i
旧时难觅i 2021-01-25 04:00

I\'m trying to escape the quotes (and apostrofes and escape char) in a text string in javascript:

var text = \'Escape \" and \\\' and /.\';
var rx = new RegExp(\         


        
相关标签:
1条回答
  • 2021-01-25 04:42

    Escaping means using backslash \ but not slash /.

    However, for your purposes you can try the following:

    text.replace(/([/'"])/g, "/$1");
    

    DEMO: http://jsfiddle.net/hvtgf/1/

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