1. 程式人生 > >js中 單引號 雙引號 引數問題 單引號 替換成 \' 雙引號 替換成 \" 等

js中 單引號 雙引號 引數問題 單引號 替換成 \' 雙引號 替換成 \" 等

.replace(/\'/g, "\\'")    ->  ' 替換成  \'

.replace(/\"/g, "\\\"")  ->  " 替換成\"   

注:適用於 onclick='Test("數字雙音\"")' 這種寫法

.replace(/\"/g, """)  ->" 替換成 " 

注:適用於onclick="Test('數字雙音&quot')"  建議寫法

.replace(/</g, "\\\<") ->  < 替換成\<

.replace(/>/g,"\\\>")  ->  >

替換成\>

正確執行的結果格式示例:

<input type="button" onclick="Test('數字\'雙音"')" value="測試" />


 function Test(s) {
            alert(s);
        }