在日常開發中,經常遇到針對字串的替換、擷取,知識點比較碎容易混淆,特此總結一下,僅供參考。
let strtest = "0123測試replace456測試replace789測試replace0"
console.log("原字串:" + strtest)
let outstr = strtest.replace("測試","ceshi")
console.log("輸出字串:" + outstr)
// 原字串:0123測試replace456測試replace789測試replace0
// 輸出字串:0123ceshireplace456測試replace789測試replace0
當直接在被替換字串恰後加上 / 時,不新增修飾符,就等同於直接字串替換。
let strtest = "0123測試replace456測試replace789測試replace0"
console.log("原字串:" + strtest)
let outstr = strtest.replace(/測試/, "ceshi")
console.log("輸出字串:" + outstr)
// 原字串:0123測試replace456測試replace789測試replace0
// 輸出字串:0123ceshireplace456測試replace789測試replace0
let strtest = '0123測試replace456測試replace789測試replace0';
console.log("原字串:" + strtest)
let arrstr = strtest.split("測試");
let outstr = arrstr.join("ceshi");
console.log("輸出字串:" + outstr)
// 原字串:0123測試replace456測試replace789測試replace0
// 輸出字串:0123ceshireplace456ceshireplace789ceshireplace0
格式:/待替換的字串/g、/待替換的字串/mgi。
g:執行全域性匹配,替換全部匹配項;
i:不區分大小寫;
m:多行匹配。
gim 可所以組合使用。
let strtest = "0123測試replace456測試replace789測試replace0"
console.log("原字串:" + strtest)
let outstr = strtest.replace(/測試/g, "ceshi")
// 另一種寫法:
// let reg = new RegExp("測試", "g")
// let outstr = strtest.replace(reg, "ceshi")
console.log("輸出字串:" + outstr)
// 原字串:0123測試replace456測試replace789測試replace0
// 輸出字串:0123ceshireplace456ceshireplace789ceshireplace0
下邊以第二個為例。
思路:先通過被替換項分組,再根據目標位數把整個分組分為兩個部分,在通過替換字串聯合。
let num = 2 // 定義替換第二個匹配項
let strtest = '0123測試replace456測試replace789測試replace0';
console.log("原字串:" + strtest)
let arrstr = strtest.split("測試");
if(arrstr.length < num)
return
let arrstr1=[],arrstr2=[]
for(let ii=0;ii<arrstr.length;ii++){
if(ii < num)
arrstr1.push(arrstr[ii])
else
arrstr2.push(arrstr[ii])
}
let outstr = arrstr1.join("測試")+"ceshi"+arrstr2.join("測試");
console.log("輸出字串:" + outstr)
// 原字串:0123測試replace456測試replace789測試replace0
// (index):59 輸出字串:0123測試replace456ceshireplace789測試replace0
將第一個匹配項跳過,並把第二個匹配項之前的內容標識為變數。
let strtest = '0123測試replace456測試replace789測試replace0';
console.log("原字串:" + strtest)
let outstr = strtest.replace(/((?:.*?測試.*?){1}.*?)測試/m, "$1-ceshi-")
// ?: 表示:其後邊的 pattern 匹配但不獲取。匹配項被標識為 $1,後續會參照
// .*? 表示:.* 除 '/n' 之外的任意個任意字元
// {1} 表示:其前邊的 pattern 出現一次
// /pattern/m 表示: m 全域性查詢
console.log("輸出字串:" + outstr)
// 原字串:0123測試replace456測試replace789測試replace0
// 輸出字串:0123測試replace456測試replace789-ceshi-replace0
參考:https://stackoverflow.com/questions/42943096/replace-nth-match-of-matches-with-regex
let strtest = '0123<Object>replace456測試replace78</Object>9測試replace0';
let startstr = "<Object>"
let endstr = "</Object>"
if (strtest.indexOf(startstr) > strtest.indexOf(endstr))
return
console.log("原字串:" + strtest)
let arr = strtest.split(startstr)
let arr2 = arr[1].split(endstr)
let resultstr = arr2[0]
let outstr = startstr + resultstr + endstr
console.log("輸出字串:" + outstr)
// 原字串:0123<Object>replace456測試replace78</Object>9測試replace0
// 輸出字串:<Object>replace456測試replace78</Object>
替換為指定字串:( replace() )
let strtest = '0123<Object>replace456測試replace78</Object>9測試replace0';
console.log("原字串:" + strtest)
let outstr = strtest.replace(/<Object>(.*?)<\/Object>/m, "-ceshi-") // \ 為跳脫字元
console.log("輸出字串:" + outstr)
// 原字串:0123<Object>replace456測試replace78</Object>9測試replace0
// 輸出字串:0123-ceshi-9測試replace0
取出帶標記字串的內容:( match() )
let strtest = '0123<Object>replace456測試replace78</Object>9測試replace0';
console.log("原字串:" + strtest)
let outstr = strtest.match(/<Object>(.*?)<\/Object>/m)
console.log("輸出字串:" + outstr)
// 原字串:0123<Object>replace456測試replace78</Object>9測試replace0
// 輸出字串:<Object>replace456測試replace78</Object>
當可能有多個匹配項時:( exec() )
let strtest = "這是<Object>要提取的內容</Object>,還有<Object>另一個內容</Object>"
console.log("原字串:" + strtest)
let regex = /<Object>(.*?)<\/Object>/gm
const matches = []
let match
while ((match = regex.exec(strtest)) !== null) {
matches.push(match[1])
console.log(match)
}
console.log("輸出全部匹配項:", matches)
// 原字串:這是<Object>要提取的內容</Object>,還有<Object>另一個內容</Object>
// (2) ["<Object>要提取的內容</Object>", "要提取的內容", index: 2, input: "這是<Object>要提取的內容</Object>,還有<Object>另一個內容</Object>", groups: undefined]
// (2) ["<Object>另一個內容</Object>", "另一個內容", index: 28, input: "這是<Object>要提取的內容</Object>,還有<Object>另一個內容</Object>", groups: undefined]
// 輸出全部匹配項: (2) ["要提取的內容", "另一個內容"]
本文來自部落格園,作者:橙子家,微訊號:zfy1070491745,有任何疑問歡迎溝通,一起成長。
轉載本文請註明原文連結:https://www.cnblogs.com/czzj/p/js_replace.html