我的字串包含不需要的 html 標簽和文本,所以我想洗掉不需要的匹配文本并獲取我需要的值:
代碼:
var mystring = "<!-- html-text: 143 --> value 1 <!-- /html-text --><!-- html-text: 144 --> | <!-- /html-text --><!-- react-text: 145 --> value 3 <!-- /html-text --><!-- html-text: 146 -->, <!-- /html-text --><!-- html-text: 147 --> value 2 <!-- /html-text --><!-- html-text: 148 --> <!-- /html-text --><!-- html-text: 149 -->value 4 <!-- /html-text -->";
mystring = mystring.replace('<!-- html-text: 143 -->','');
console.log('str' mystring);
所需輸出:
value 1 value 2 value 3 value 4
uj5u.com熱心網友回復:
您可以使用正則運算式執行此操作:
var mystring = "<!-- html-text: 143 --> value 1 <!-- /html-text --><!-- html-text: 144 --> | <!-- /html-text --><!-- react-text: 145 --> value 3 <!-- /html-text --><!-- html-text: 146 -->, <!-- /html-text --><!-- html-text: 147 --> value 2 <!-- /html-text --><!-- html-text: 148 --> <!-- /html-text --><!-- html-text: 149 -->value 4 <!-- /html-text -->";
mystring = mystring.replace(/<!--.*?-->/g,'');
console.log(mystring);
正則運算式解釋:
<!--...-->標記 HTML 注釋的開始和結束.*匹配任何字符零次或多次?洗掉“貪婪匹配”(匹配最少而不是最多)g全域,意思是替換所有出現而不是一個
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/433724.html
上一篇:無法將json解碼的字串轉換為php中的陣列,這是通過ajax方法發送的
下一篇:使用jQuery應用用戶輸入更改
