Escaping HTML entities in JavaScript string literals within the [removed] block

前端 未结 5 742

On the one hand if I have

\';
console.log(s);

the browser will terminate the

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 05:13

    Here's how I do it:

    function encode(r){
    return r.replace(/[\x26\x0A\<>'"]/g,function(r){return"&#"+r.charCodeAt(0)+";"})
    }
    
    var myString='Encode HTML entities!\n"Safe" escape 
    
                                     
                  
    提交回复
热议问题