数字、小数和分数的正则表达式



如何创建允许整数、小数、分数和带小数的分数的正则表达式?字符串也可以有可选的文本,仅在末尾。到目前为止,我有这个:

const re = /^d*.?d*/?d*.?d*[a-z]*$/gi;

这允许在一个整数中使用双小数(即:"23.23.23"(,这是我不想要的。只有用"/"分隔时,我才能修改此正则表达式以允许两个小数点吗?

以下是一些可以通过的示例:

  • 23.23/100km
  • 1/3
  • 0.23公里
  • 1.英里
  • 1.2/2.1千克

一些不应该通过的例子:

  • 1a3km
  • 12.12.12
  • 1.2.3/12.13公里
  • 12公里/12.44公里

使用

^(?!.*d+(?:.d+){2})d*.?d*/?d*.?d*[a-z]*$

见证明。由于(?!.*d+(?:.d+){2})负先行,此表达式不允许三个彼此之间具有周期的数字。

解释

--------------------------------------------------------------------------------
^                        the beginning of the string
--------------------------------------------------------------------------------
(?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
.*                       any character except n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
d+                      digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
(?:                      group, but do not capture (2 times):
--------------------------------------------------------------------------------
.                       '.'
--------------------------------------------------------------------------------
d+                      digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
){2}                     end of grouping
--------------------------------------------------------------------------------
)                        end of look-ahead
--------------------------------------------------------------------------------
d*                      digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
.?                      '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
d*                      digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
/?                      '/' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
d*                      digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
.?                      '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
d*                      digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
[a-z]*                   any character of: 'a' to 'z' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
$                        before an optional n, and the end of the
string

这应该能在中工作

^[0-9]*(?:.[0-9]*)?(?:/[0-9]*(?:.[0-9]*)?)?[a-zA-Z]*$

您尝试的模式由所有可选部分组成,因为所有量词要么是表示可选的?,要么是表示0次或更多次的*。由于/也是可选的,您将匹配12.12.12

您可以使带有/部件的部件成为可选部件,而不必使/本身成为可选部件。

如果您希望至少匹配一个数字并防止匹配空字符串,则可以使用正向前瞻来匹配至少一个数字。

^(?=[^drn]*d)d*.?d*(?:/d*.?d*)?[a-z]*$

Regex演示

图案与匹配

  • ^字符串开始
  • (?=[^drn]*d)断言至少一个数字
  • d*.?d*匹配可选数字和.
  • (?:非捕获组
    • /d*.?d*匹配/和可选数字以及.
  • )?关闭非捕获组并将其设为可选
  • [a-z]*可选匹配字符a-z
  • $字符串末尾

如果/的每侧至少有一个数字不匹配,例如./.

^(?:d*.?d+|d+.)(?:/d*.?d+|d+.)?[a-z]*$

图案与匹配

  • ^字符串开始
  • (?:d*.?d+|d+.)匹配任意可选数字、可选.和1+数字或匹配1+数字和.
  • (?:非捕获组
    • /d*.?d+|d+.匹配/,再次与第一个图案相同
  • )?关闭非捕获组并将其设为可选
  • [a-z]*可选匹配字符a-z
  • $字符串结束

Regex演示

最新更新