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

Regex: only single instance of character in negated set

有什么方法可以将否定集中的字符限制为单个实例吗?

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

这不会匹配数字和小数,但是有什么方法可以限制为单个小数吗?

所以它不会匹配:

84.34356
.3948

但会匹配:

86..3232
   ^
84.54.23.
     ^  ^

这边

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

将替换匹配的字符

使用 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"

描述

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

替换为: </code></p> <p><WBIMG:3996444-1.png></p> <p>此正则表达式将执行以下操作:</p> <ul> <li>替换数字中或周围不需要的小数点</li> <li>在常规文本周围保留小数点</li> </ul> <h1>例子</h1> <p><strong>现场演示</strong></p> <p><a href="https://regex101.com/r/pF4aS3/2" rel="nofollow">https://regex101.com/r/pF4aS3/2</a></p> <p><strong>示例文本</strong></p> <pre><code>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 :
----------------------------------------------------------------------
    [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 
----------------------------------------------------------------------
 |                        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
----------------------------------------------------------------------