我正在嘗試替換字串中存在的特定字母,但問題是我要替換多個字母,因此我必須手動撰寫所有替換代碼
例如:-
let string= "<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query>"
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
let string= "<!-- ct:hold --><!--start-->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not <!--end-->only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with <!--start-->desktop publishing software like<!--Fragrement--> Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query -->";
string =string.replace(/<!--?\s \/?ct:.*?-->/g,"");
console.log(string)
如這里您可以看到這是我要替換的一些小值,但是我要洗掉一些大關鍵字
例如:-
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
.replace(/<!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->/)
.replace(/<!-- ct:spacer {\"height\":120} -->/)
有沒有可能我只能洗掉以開頭的值,<!-- ct:如果以結尾的值-->和替換文本的長度無關緊要
uj5u.com熱心網友回復:
除了已經與上述評論相關聯的解決方案/變體之外,還有第 3 個變體,它在語法上匹配/有效評論方面不太嚴格,例如 OP 示例資料中的最后一個。
下面的代碼顯示了使用 ...
變體 2 ...
/<!--?\s \/?ct:.*?-->/g變體 3 ...
/<!--?\s \/?ct:.*?>/g
const sampleText = `
<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry.
<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {"queryId":8,"query":{"perPage":"10","pages":0,"offset":0,"postType"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of
Lorem Ipsum.<!-- /ct:query>`;
// see ... [https://regex101.com/r/olNflD/2]
const regXComment2 = /<!--?\s \/?ct:.*?-->/g;
// see ... [https://regex101.com/r/olNflD/3]
const regXComment3 = /<!--?\s \/?ct:.*?>/g;
console.log(
sampleText.replace(regXComment2, '')
);
console.log(
sampleText.replace(regXComment3, '')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
uj5u.com熱心網友回復:
您可以使用正則運算式通配符匹配 ( .) 并重復 1 次或多次修飾符 ( ) 以匹配 ct: 部分之后的注釋中的任何字符。同樣根據@MonkeyZeus 的評論,?需要一個惰性量詞 ( ),以便它從第一個評論的開頭一直到字串中最后一個評論的結尾都不匹配:/<\!-- ct:. ? -->/g。
uj5u.com熱心網友回復:
string.replace(/^<\!-- ct:.*-->$/gi, "");
更新:
根據@MonkeyZeus 的建議,如果您的字串中有多個要匹配的子字串,如果沒有延遲評估,您可能會得到非常糟糕的結果,這樣更安全:
string.replace(/<\!-- ct:.*? -->/gi, "");
這將匹配字串中的所有單個子字串:https ://regex101.com/r/DxwRD6/1
uj5u.com熱心網友回復:
或者,如果您不想使用正則運算式或替換,您可以使用DOMParser,然后只回傳不包含評論的文本內容
let string = "<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query>"
const doc = new DOMParser().parseFromString(string, 'text/html')
console.log(doc.all[0].textContent)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459148.html
標籤:javascript 节点.js 正则表达式 细绳 代替
