我有這個字串
這是一些文本(和文本),這是另一個文本(和文本),完成在這里。
我需要在 JavaScript 中使用正則運算式將所有出現的字串“文本(和文本)”替換為“NewText”(不區分大小寫)。所以最終的結果應該是
這是一些NewText,這是另一個NewText,補全在這里。
uj5u.com熱心網友回復:
您只需要轉義括號:這是正則運算式 /text (and text)/ig
\( -> 表示括號應按原樣匹配
i -> 不區分大小寫
g -> 全域
console.log('This is some text (and text), and this is another text (AND TexT), the completion goes here'.replaceAll(/text \(and text\)/ig, 'NewText'))
uj5u.com熱心網友回復:
const search = 'text (and text)';
const replaceWith = 'NewText';
const result = 'This is some NewText, and this is another NewText, the completion goes here.'.split(search).join(replaceWith);
console.log(result);
uj5u.com熱心網友回復:
let string = "This is some text (and text), and this is another text (AND TexT), the completion goes here.";
console.log(string.replace( /(\w ) \(.*?\)/gi , "NewText"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491889.html
標籤:javascript 正则表达式
