試圖用dot替換數字前面的特殊字符。
const time = "17:34:12:p. m." 。
const output = time.replace(/d (.)/g, '. ' )。
//預期輸出 "17.34.12.p. m.".
console.log(output);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
我寫了一個詞條,它將捕獲任何以數字為首的字符。輸出的結果是將數字也替換掉了。誰能幫我解決這個問題呢?
uj5u.com熱心網友回復:
你可以使用
。const time = "17:34:12:p. m." ;
const output = time.replace(/(d)[W_]/g, '1. ')。
console.log(output);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
time.replace(/(d)[W_]/g, '$1.')代碼將匹配并捕獲一個數字到第1組,并匹配任何非字或下劃線的字符,而$1.替換將把數字放回去,并用.替換:。
如果您想從[W_]中 "減去 "空白模式,請使用(?:[^ws]|_)。
考慮在檢查字串中的特殊字符模式中檢查更多的特殊字符模式。
uj5u.com熱心網友回復:
你應該尋找非單詞(w)和非空格(s)字符,并用點來替換它們。
你應該使用一些正則運算式的實時模擬程式。例如,regex101。https://regex101.com/r/xIStHH/1
const time = "17:34:12:p. m." 。
const output = time.replace(/[^ws]/g, '. ' )。
//預期輸出 "17.34.12.p. m."。
console.log(output);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/307547.html
標籤:
上一篇:匹配不同后綴和后綴的運算式
下一篇:帶有特殊字符的用戶名的正則運算式
