简体   繁体   English

正则表达式仅在 Javascript 中没有跟在或后面没有字母的情况下才匹配数字

[英]Regex to match digits only if not followed or proceeded by letters in Javascript

I would like to match digits but not when they are within words (in JavaScript).我想匹配数字,但不匹配单词(在 JavaScript 中)。

The following should match:以下应匹配:

1
1,2
1.5-4 (matches 1.5 & 4 separately)
(1+3) (matches 1 & 3 separately)
=1;

The following should NOT match:以下不应该匹配:

FF3D
3deg

I thought I could solve with with a negative lookahead, like so: (?![A-Za-z]+)([0-9]+[\\.|\\,]?[0-9]?) but it does not work.我以为我可以用负向预测来解决,就像这样: (?![A-Za-z]+)([0-9]+[\\.|\\,]?[0-9]?)但它不起作用。

How can I best solve this?我怎样才能最好地解决这个问题? Thanks.谢谢。

I would like to match digits but not when they are within words.我想匹配数字,但不匹配单词。

You can use look arounds in your regex:您可以在正则表达式中使用环视:

\b\d*[,.]?\d+\b
  • \\b is for word boundary \\b用于单词边界

RegEx Demo正则表达式演示

2021 update: 2021年更新:

Since lookbehind support has grown considerably, it makes sense to use a lookbehind based solution:由于后视支持已显着增长,因此使用基于后视的解决方案是有意义的:

/(?<![a-z])\d*[.,]?\d+(?![a-z])/gi         # ASCII only
/(?<!\p{L})\p{N}*[.,]?\p{N}+(?!\p{L})/giu  # Unicode-aware

See the regex demo .请参阅正则表达式演示 Please track the lookbehind and Unicode property class support here .请在此处跟踪后视和 Unicode 属性类支持

Details细节

  • (?<![az]) - no ASCII letter (or any Unicode letter if \\p{L} is used) allowed immediately to the left of the current location (?<![az]) - 当前位置的左侧不允许有 ASCII 字母(或任何 Unicode 字母,如果使用\\p{L}
  • \\d*[.,]?\\d+
  • (?![az]) - no ASCII letter (or any Unicode letter if \\p{L} is used) allowed immediately to the right of the current location. (?![az]) - 当前位置的右侧不允许有 ASCII 字母(或任何 Unicode 字母,如果使用\\p{L} )。

Original answer原答案

In order to match any standalone integer or float numbers with dot or comma as decimal separator you need为了匹配任何独立的整数或浮点数,您需要使用点或逗号作为小数点分隔符

/(?:\b\d+[,.]|\B[.,])?\d+\b/g

See the regex demo .请参阅正则表达式演示 The point here is that you cannot use a word boundary \\b before a .这里的要点是您不能在\\b之前使用单词边界\\b . since it will invalidate all matches like .55 (only 55 will be matched).因为它会使所有匹配项无效,例如.55 (仅匹配55 )。

Details :详情

  • (?:\\b\\d+[,.]|\\B[.,])? - either of the two alternatives: - 两种选择之一:
    • \\b\\d+[,.] - a word boundary (there must be a non-word char before or start of string), then 1+ digits, and then a . \\b\\d+[,.] - 一个单词边界(在字符串之前或开始之前必须有一个非单词字符),然后是 1+ 位数字,然后是一个. or ,,
    • | - or - 或者
    • \\B[.,] - a position other than word boundary (only a non-word char or start of string) and then a . \\B[.,] - 单词边界以外的位置(仅非单词字符或字符串开头),然后是. or ,,
  • \\d+ - 1+ digits \\d+ - 1+ 位数
  • \\b - a word boundary. \\b - 单词边界。

 const regex = /(?:\\b\\d+[,.]|\\B[.,])?\\d+\\b/g; const str = `.455 and ,445 44,5345 435.54 4444 1 1,2 1.5-4 (1+3) =1; FF3D 3deg`; console.log(str.match(regex));

If you need to also add support for the exponent use:如果您还需要添加对指数的支持,请使用:

/(?:\b\d+[,.]|\B[.,])?\d+(?:e[-+]?\d+)?\b/ig

Try This尝试这个

var pattern= /([\d.]+)/;

https://regex101.com/r/q1qDHV/1 https://regex101.com/r/q1qDHV/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM