How do I replace a double-quote with an escape-char double-quote in a string using JavaScript?

前端 未结 4 1263
臣服心动
臣服心动 2020-12-29 01:57

Say I have a string variable (var str) as follows-

Dude, he totally said that "You Rock!"

Now If I\'

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 02:14

    You need to use a global regular expression for this. Try it this way:

    str.replace(/"/g, '\\"');
    

    Check out regex syntax and options for the replace function in Using Regular Expressions with JavaScript.

提交回复
热议问题