Escape quotes in JavaScript

前端 未结 13 853
不思量自难忘°
不思量自难忘° 2020-11-22 11:39

I\'m outputting values from a database (it isn\'t really open to public entry, but it is open to entry by a user at the company -- meaning, I\'m not worried about XSS).

13条回答
  •  无人共我
    2020-11-22 12:00

    This is how I do it, basically str.replace(/[\""]/g, '\\"').

    var display = document.getElementById('output');
    var str = 'class="whatever-foo__input" id="node-key"';
    display.innerHTML = str.replace(/[\""]/g, '\\"');
    
    //will return class=\"whatever-foo__input\" id=\"node-key\"

提交回复
热议问题