首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

JS 批量替換  :正則全局忽略大小寫 replace

     編輯:程式設計綜合問答

代碼


const html = 'a b   c  d ';
const clean = html.replace(/ /gi, '');
console.log(clean); // "ab c d"

正則解析

  • / / 匹配實體字符串。
  • g 全局匹配(所有出現)。
  • i 忽略大小寫(兼容   等)。

擴展:替換所有 HTML 實體


function decodeHtmlEntities(str) {
    const textarea = document.createElement('textarea');
    textarea.innerHTML = str;
    return textarea.value;
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved