4 回答

TA貢獻1808條經驗 獲得超4個贊
let str = 'Test 2 >> Cat >> Cat2 >> Cat4 || //// Cat /// Cat2 // Cat4 // CCat / CCat2 // CCat3 ///';
res = str.replace(/(?:>|>){2,}|\/\/+/g, '/');
console.log(res);
(?:>|>){2,}
是匹配2個或更多“大于”>
或html實體的非捕獲組>
\/\/+
匹配 2 個或更多斜杠

TA貢獻1883條經驗 獲得超3個贊
以下是我將如何做到這一點:您將需要replace()使用一個正則表達式來調用,該正則表達式在全局范圍內匹配所有>>使用g修飾符的事件:
let newText = oldText.replace(/>>/g, "/");
let btn = document.querySelector("#start");
let span = document.querySelector("#catidX");
btn.addEventListener("click", function() {
let oldText = span.innerText;
let newText = oldText.replace(/>>/g, "/");
span.innerText = newText;
});
<span id="catidX"> Test 2 >> Cat >> Cat2 >> Cat4 || //// Cat /// Cat2 // Cat4 // CCat / CCat2 // CCat3 ///</span>
<br />
<button id="start">Replace</button>

TA貢獻1809條經驗 獲得超8個贊
試試這個代碼
var str = "Test 2 >> Cat >> Cat2 >> Cat4"
str.replace(/>>/gi, "/");
result : "Test 2 / Cat / Cat2 / Cat4"
添加回答
舉報