[英]Regex: Match a character that is unmatched in a negated set only a certain number of times at the end of a string
[英]Regex: only single instance of character in negated set
有沒有可能將否定集中的字符限制為單個實例的方法?
我正在使用的工具:[^ 0-9 \\。]
這不會匹配數字和小數,但是有什么方法可以限制為一個小數嗎?
所以它不會匹配:
84.34356
.3948
但將匹配:
86..3232
^
84.54.23.
^ ^
這條路
val.replace(/[^0-9\.]/g, '')
將替換匹配的字符
使用split
, shift
和join
:
var s = '84.54.23.'
var arr = s.split(/\./).filter(Boolean)
if (arr.length > 1)
s = arr.shift() + '.' + arr.join('')
//=> s="84.5423"
s = '86..3232'
arr = s.split(/\./).filter(Boolean)
if (arr.length > 1)
s = arr.shift() + '.' + arr.join('')
//=> s="86.3232"
s = '84.34356'
arr = s.split(/\./).filter(Boolean)
if (arr.length > 1)
s = arr.shift() + '.' + arr.join('')
//=> s="84.34356"
s = '.3948'
arr = s.split(/\./).filter(Boolean)
if (arr.length > 1)
s = arr.shift() + '.' + arr.join('')
//=> s=".3948"
^([0-9]+[.](?=[.]+)|[0-9]+[.][0-9]+|[.][0-9]+)|(?<=[0-9])[.]|[.](?=[0-9]|$)
替換為: $1
此正則表達式將執行以下操作:
現場演示
https://regex101.com/r/pF4aS3/2
示范文本
84.34356
.3948
but will match.:
86..3232
84.54.23.
更換后
84.34356
.3948
but will match.:
86.3232
84.5423
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[.]+ any character of: '.' (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?<= look behind to see if there is:
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.