简体   繁体   English

正则表达式 - 4 位数字,可选 1 或 2 位小数

[英]Regular Expression - 4 digits, with optional 1 or 2 decimal places

Have a regular expression issue that I can't seem to quite get.有一个我似乎不太明白的正则表达式问题。

Requirements: - Value between 0-9999 - Optionally can add either one or two decimals ( eg, 0.01 - 9999.99)要求: - 0-9999 之间的值 - 可选择添加一位或两位小数(例如,0.01 - 9999.99)

I've got a regex test and it looks to pass but when using it in my SAPUI5 app it doesn't seem to be working.我有一个正则表达式测试,它看起来通过了,但是在我的 SAPUI5 应用程序中使用它时,它似乎不起作用。

https://regex101.com/r/kB7oJ2/13 https://regex101.com/r/kB7oJ2/13

JS code: JS代码:

var iQuantity = parseFloat(oArticle._Quantity);
var regexp = new RegExp('^([0-9]{1,4})(\.[0-9]{1,2})?$').test(iQuantity);

console.log(iQuantity);
console.log(regexp);

if (regexp === false) {
     return this.setItemToError(oInput, oArticle, 
     this.getResourceBundle().getText("regExp"));
}

Something like this perhaps:可能是这样的:

var re = /^(([0-9]{1,4})(\.[0-9]{1,2})?)$/;
var match = re.exec(subject);
if (match != null) {
    result = match[1];
} else {
    result = "";
}

/^\d{1,digits_before_decimal}$|(?=^.{1,char_count_with_decimal}$)^\d{0,digits_before_decimal}.\d{0,digits_after_decimal}$/; /^\d{1,digits_before_decimal}$|(?=^.{1,char_count_with_decimal}$)^\d{0,digits_before_decimal}.\d{0,digits_after_decimal}$/;

In your example it is在你的例子中是

/^\d{1,4}$|(?=^.{1,7}$)^\d{0,4}.\d{0,2}$/ /^\d{1,4}$|(?=^.{1,7}$)^\d{0,4}.\d{0,2}$/

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

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