javascript 匹配字符串中的数字但不匹配百分比的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13212771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:05:36  来源:igfitidea点击:

Regular expression that matches number in string but not percentages

javascriptregex

提问by lorenzo-s

I need to know if there is a regular expression for testing for the presence of numbers in strings that:

我需要知道是否有用于测试字符串中是否存在数字的正则表达式:

  • Matches Lorem 20 Ipsum
  • Matches Lorem 2,5 Ipsum
  • Matches Lorem 20.5 Ipsum
  • Does notmatch Lorem 2% Ipsum
  • Does notmatch Lorem 20.5% Ipsum
  • Does notmatch Lorem 20,5% Ipsum
  • Does notmatch Lorem 2 percent Ipsum
  • Does notmatch Lorem 20.5 percent Ipsum
  • Does notmatch Lorem 20,5 percent Ipsum
  • Matches Lorem 20 Ipsum 2% dolor
  • Matches Lorem 2,5 Ipsum 20.5% dolor
  • Matches Lorem 20.5 Ipsum 20,5% dolor
  • 火柴 Lorem 20 Ipsum
  • 火柴 Lorem 2,5 Ipsum
  • 火柴 Lorem 20.5 Ipsum
  • 难道匹配Lorem 2% Ipsum
  • 难道匹配Lorem 20.5% Ipsum
  • 难道匹配Lorem 20,5% Ipsum
  • 难道匹配Lorem 2 percent Ipsum
  • 难道匹配Lorem 20.5 percent Ipsum
  • 难道匹配Lorem 20,5 percent Ipsum
  • 火柴 Lorem 20 Ipsum 2% dolor
  • 火柴 Lorem 2,5 Ipsum 20.5% dolor
  • 火柴 Lorem 20.5 Ipsum 20,5% dolor

That is, a regular expression that can tell me if in a string there is one or many numbers, but not as percentage value.

也就是说,一个正则表达式可以告诉我在一个字符串中是否有一个或多个数字,但不能作为百分比值。

I've tried something as /[0-9\.,]+[^%]/, but this not seems to work, I think because digits then not a percentage signmatch also the 20in the string 20%. Additionally, I don't know how to tell not the entire percentstringin addition to the %char.

我已经尝试了一些东西/[0-9\.,]+[^%]/,但这似乎不起作用,我认为因为数字然后不是百分比符号也匹配20字符串中的20%。此外,我不知道除了字符之外如何分辨整个percent字符串%

回答by Rob Raisch

This will do what you need:

这将满足您的需求:

\b                     -- word boundary
\d+                    -- one or more digits
(?:\.\d+)?             -- optionally followed by a period and one or more digits
\b                     -- word boundary
\s+                    -- one or more spaces
(?!%|percent)          -- NOT followed by a % or the word 'percent'

--EDIT--

- 编辑 -

The meat here is the use of a "negative lookahead" on the final line that causes the match to fail if any of a percent-sign or the literal "percent" occurs after a number and one or more spaces. Other uses of negative lookahead in JavaScript RegExps can be found at Negative lookahead Regular Expression

这里的主要内容是在最后一行使用“负前瞻”,如果在数字和一个或多个空格之后出现任何百分号或文字“百分比”,则会导致匹配失败。在 JavaScript RegExps 中负前瞻的其他用法可以在负前瞻正则表达式中找到

--2ND EDIT-- Congrats to Enrico for solving the most general case but while his solution below is correct, it contains several extraneous operators. Here is the most succinct solution.

--第二次编辑--祝贺 Enrico 解决了最一般的情况,虽然他的解决方案是正确的,但它包含几个无关的运算符。这是最简洁的解决方案。

(                         -- start capture
  \d+                     -- one or more digits
  (?:[\.,]\d+)?           -- optional period or comma followed by one or more digits
  \b                      -- word boundary
  (?!                     -- start negative lookahead
    (?:[\.,]\d+)          -- must not be followed by period or comma plus digits
  |                       --    or
    (?:                   -- start option group
      \s?%                -- optional space plus percent sign
    |                     --   or
      \spercent           -- required space and literal 'percent'
    )                     -- end option group
  )                       -- end negative lookahead
)                         -- end capture group

回答by enrico.bacis

This is the robust way to do it, and it's also extracting the numbers.

这是做到这一点的可靠方法,而且它还可以提取数字。

(\b\d+(?:[\.,]\d+)?\b(?!(?:[\.,]\d+)|(?:\s*(?:%|percent))))

It is similar to Rob's regex but it should work for all the cases.

它类似于 Rob 的正则表达式,但它应该适用于所有情况。

(                          -- capturing block
  \b                       -- word boundary
  \d+                      -- one or more digits
  (?:[\.,]\d+)?            -- optionally followed by a period or a comma
                              and one or more digits
  \b                       -- word boundary
  (?!                      -- not followed by
    (?:[\.,]\d+)           -- a period or a comma and one or more digits
                              [that is the trick]
    |                      -- or
    (?:\s*(?:%|percent))   -- zero or more spaces and the % sign or 'percent'
  )
)

回答by Bergi

Use negative lookaheadinstead of your negated character class:

使用否定前瞻代替否定字符类:

/\d+(?:[,.]\d+)?(?!\s*(?:percent|%))/