有效值:
50,
50.00,
(50),
(2),
(50.00),
(10.XX),
一些无效的例子:
(50,
((50,
50)),
60),
50.),
答案 0 :(得分:0)
答案 1 :(得分:0)
我假设格式是##。##,最多两位数,可选小数可能还有一两个数字,整个shebang可能被parens包围以表示否定。我还假设10.XX并不是真的有效,但用数字代替X将是。
我建议使用单独的正面和正面正则表达式,以便您的代码清晰明了。 E.g:
good = ["50", "50.00", "(50)", "(2)", "(50.00)", "(10.19)"];
bad = ["(50", "((50", "50))", "60)", "50.)"];
posRegex = /^\d\d?(\.\d{0,2})?$/;
negRegex = /^\(\d\d?(\.\d{0,2})?\)$/;
function matches(val) {
var m = posRegex.test(val) || negRegex.test(val);
console.log(val + " : " + m);
}
console.log("These should be true");
good.forEach(matches);
console.log("These should be false");
bad.forEach(matches);