![](/img/trans.png)
[英]Remove all occurrences of a character except the first one in string with javascript
[英]Replace all occurrences of character except in the beginning of string (Regex)
我正在嘗試消除字符串中所有的負號/破折號,但第一次出現的除外。 擺弄Regex(JavaScript)半小時后,仍然沒有結果。 有人知道解決辦法嗎?
鑒於:
-123-45-6
預期:
-123456
鑒於:
789-1-0
預期:
78910
這也將做(這意味着破折號不在字符串的開頭):
(?!^)-
例:
text = "-123-45-6".replace(/(?!^)-/g, "");
一個簡單的解決方案:
s = s.replace(/(.)-/g,'$1')
Jutr嘗試:
'-123-45-6'.replace(/(\d)-/g, '$1');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.