簡體   English   中英

RegExp將替換不屬於十進制值的任何匹配單詞

[英]RegExp that will replace any matching word which is not part of a decimal value

到目前為止,對我來說,從字符串中查找和刪除單詞一直很有效,但不幸的是,它還匹配數字的個位數,該數字是較大的十進制數字的一部分。 如何修改Reg Expr以使其不匹配十進制數字,但要處理其他任何非數字出現的“。”。 作為文字解析器? `

 $("#123").val(removeSubstring("this should be removed: 9. But not 9.9 or 1.9 or 9.1 or 999 but it should remove this 9 and this 9,too!","9")); function removeSubstring(lookIn,subStringToRemove){ return lookIn.replace(new RegExp("\\\\b" + subStringToRemove + "\\\\b","gi"),""); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id=123 style="width: 100%;"></input> 

所需的輸出應為“ this should be removed: . But not 9.9 or 1.9 or 9.1 or 999 but it should remove this and this ,too! 因此,僅應刪除“ 9”中的兩個。

如果要刪除任意數量的數字,然后刪除點號或逗號,並保留點號和逗號,則此方法將為您工作:

data.replace(/\d+(\.|\,\D?)/g, '$1')

這個怎么運作

\\d+查找任意數量的數字。
\\.|\\,找到“。” 要么 ','
\\D? 查找一個或零個非數字字符。
()創建捕獲組。

$1在替換時使用第一個捕獲組的內容。

檢查它是否適合您。

使用負向后看

(?<!\.|\d)9(?!\.\d|\d)

https://regex101.com/r/an8eg1/1

但這在javascript中不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM