javascript - 用于验证整数或带两位小数的 float 的正则表达式

标签 javascript regex regex-lookarounds regex-group regex-greedy

我愿意得到一个 regexp 以允许下面的价格格式(这是一个表单中的string)

允许

  • (任何不带小数的价格。可以是 [0-9] 1 次或多次)
  • ,#(任何带一位小数的价格)
  • ,##(带两位小数的任何价格)

不允许

  • 任何其他...例如:
  • 5,555...(3 个 decmail 或更长)
  • 任何包含超过 1 个逗号的字符串
  • 任何包含不是数字的字符的字符串

到目前为止我得到了这个:

/^[0-9]+(,[0-9][0-9]?)?$/

这似乎是有效的。但它真的如我所愿吗?

const priceRegex = /^[0-9]+(,[0-9][0-9]?)?$/;

const span1 = document.getElementById('span1');
const span2 = document.getElementById('span2');
const span3 = document.getElementById('span3');
const span4 = document.getElementById('span4');
const span5 = document.getElementById('span5');
const span6 = document.getElementById('span6');
const span7 = document.getElementById('span7');
const span8 = document.getElementById('span8');
const span9 = document.getElementById('span9');
const span10 = document.getElementById('span10');
const span11 = document.getElementById('span11');
const span12 = document.getElementById('span12');
const span13 = document.getElementById('span13');

span1.innerHTML = priceRegex.test('5') ? ' IS VALID' : ' IS NOT VALID';
span2.innerHTML = priceRegex.test('5,5') ? ' IS VALID' : ' IS NOT VALID';
span3.innerHTML = priceRegex.test('5,55') ? ' IS VALID' : ' IS NOT VALID';

span4.innerHTML = priceRegex.test('5,') ? ' IS VALID' : ' IS NOT VALID';
span5.innerHTML = priceRegex.test('5,555') ? ' IS VALID' : ' IS NOT VALID';
span6.innerHTML = priceRegex.test('5,5555') ? ' IS VALID' : ' IS NOT VALID';

span7.innerHTML = priceRegex.test('a') ? ' IS VALID' : ' IS NOT VALID';
span8.innerHTML = priceRegex.test('5a') ? ' IS VALID' : ' IS NOT VALID';
span9.innerHTML = priceRegex.test('a5') ? ' IS VALID' : ' IS NOT VALID';

span10.innerHTML = priceRegex.test('a,5') ? ' IS VALID' : ' IS NOT VALID';
span11.innerHTML = priceRegex.test('5,a') ? ' IS VALID' : ' IS NOT VALID';
span12.innerHTML = priceRegex.test('a5,5') ? ' IS VALID' : ' IS NOT VALID';
span13.innerHTML = priceRegex.test('5a,5') ? ' IS VALID' : ' IS NOT VALID';
div {
  color: black;
}

span {
  color: blue;
}

.notValid {
  color: red;
}
<div>5<span id="span1"></span><div>
<div>5,5<span id="span2"></span></div>
<div>5,55<span id="span3"></span></div>

<br/>

<div>5,<span id="span4" class="notValid"></span><div>
<div>5,555<span id="span5" class="notValid"></span></div>
<div>5,5555<span id="span6" class="notValid"></span></div>

<div>a<span id="span7" class="notValid"></span></div>
<div>5a<span id="span8" class="notValid"></span></div>
<div>a5<span id="span9" class="notValid"></span></div>
 
<div>a,5<span id="span10" class="notValid"></span></div>
<div>5,a<span id="span11" class="notValid"></span></div>
<div>a5,5<span id="span12" class="notValid"></span></div>
<div>5a,5<span id="span13" class="notValid"></span></div>

最佳答案

在这里,我们可能会从两个简单的表达式开始,然后用逻辑或将它们连接起来,可能类似于:

^[0-9]+$|^[0-9]+,[0-9]{1,2}$

DEMO

测试

const regex = /^[0-9]+$|^[0-9]+,[0-9]{1,2}$/gm;
const str = `5
5,5
5,55

5,
5,555
5,5555
a
5a
a5
a,5
5,a
a5,5
5a,5`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

正则表达式电路

jex.im可视化正则表达式:

enter image description here

正则表达式

如果不需要此表达式,可以在 regex101.com 中对其进行修改/更改.

关于javascript - 用于验证整数或带两位小数的 float 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56380402/

相关文章:

python - 我需要使用什么模式来分割字符?

javascript - 正则表达式和 ISO8601 格式的日期时间

javascript - 向 Rails 模型提交值

javascript - 如何在不知道图像 url 的情况下向现有背景图像添加叠加层?

javascript - 显示在网站上的倒计时器

Java正则表达式单词提取排除特殊字符

c# - 包含 "|"的正则表达式

regex - 当组 1、2、3 或 4 之间没有实例时,如何匹配 (group2.*|^.*)group1?

regex - 匹配没有任何 2 个连续重复字符的字符串的正则表达式模式

javascript - Node.js 基于子域的重定向样式和资源文件夹