[英]Regex: only single instance of character in negated set
Is there any possible way to limit a character in a negated set to a single instance? 有没有可能将否定集中的字符限制为单个实例的方法?
What I am working with: [^0-9\\.] 我正在使用的工具:[^ 0-9 \\。]
This wont match numbers and decimals, but is there any way to limit to a single decimal? 这不会匹配数字和小数,但是有什么方法可以限制为一个小数吗?
So it wont match: 所以它不会匹配:
84.34356
.3948
but will match: 但将匹配:
86..3232
^
84.54.23.
^ ^
This way 这条路
val.replace(/[^0-9\.]/g, '')
would replace the matched characters 将替换匹配的字符
Using split
, shift
and join
: 使用
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]|$)
Replace with: $1
替换为:
$1
This regular expression will do the following: 此正则表达式将执行以下操作:
Live Demo 现场演示
https://regex101.com/r/pF4aS3/2 https://regex101.com/r/pF4aS3/2
Sample text 示范文本
84.34356
.3948
but will match.:
86..3232
84.54.23.
After Replacement 更换后
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.