2016-05-26 17 views
0

是否有任何可能的方法將否定集中的字符限制爲單個實例?正則表達式:只有單個實例的字符集否定集

我所用的工作: [^ 0-9 \。]

這不會匹配數字和小數點,但沒有任何辦法來限制爲單個十進制?

所以它不會匹配:

84.34356 
.3948 

但將匹配:

86..3232 
^
84.54.23. 
    ^^ 

這樣

val.replace(/[^0-9\.]/g, '') 

將取代匹配的字符

+0

所以'86..3232'是一個有效的輸入? – anubhava

+0

我想匹配第二個'。'所以它可以很容易地被刪除。 – Alex

+0

然後只匹配['^(\ d +(?:\。\ d +)?)$'](https://regex101.com/r/iC3xV5/1) – Tushar

回答

0

說明

^([0-9]+[.](?=[.]+)|[0-9]+[.][0-9]+|[.][0-9]+)|(?<=[0-9])[.]|[.](?=[0-9]|$) 

替換:$1

Regular expression visualization

這個正則表達式將執行以下操作:

或周圍的數字發現
  • 更換不必要的小數點
  • 亂放常規文本

現場演示

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 
---------------------------------------------------------------------- 
1

使用splitshiftjoin

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"