正则表达式:在否定集中只有单个字符实例

时间:2016-05-26 15:38:48

标签: javascript regex regex-negation

有没有办法将否定集中的字符限制为单个实例?

我在做什么: [^ 0-9 \。]

这不会匹配数字和小数,但有没有办法限制为一个小数?

所以它不匹配:

84.34356
.3948

但会匹配:

86..3232
   ^
84.54.23.
     ^  ^

这样

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

将替换匹配的字符

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分: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
----------------------------------------------------------------------