[英]Javascript RegExp for matching different values with $ (dollar sign) as part of the string
我正在寻找一种从用户输入值构造RegExp对象的方法,该输入值将与某些包含美元字符$(货币)的值匹配。
据我所知,必须逃脱此字符,我在使其正常工作时遇到了问题,尤其是与其他运算符和字符结合使用时。
我正在寻找一种可根据用户输入工作的RegExp,因此它需要动态。 尝试以这种方式解释场景:
用户输入: "$50"
或"50"
"$50" // match
"$50 + $5" // match as it matches on "$50"
"$500 + $50" // match as it matches on "$50"
"$500" // should not match
"$5000" // should not match
"$5.50" // should not match
用户输入: "$5000"
或"5000"
"$5000" // match
"$5,000" // match as it resembles the same value
"$5000 + $500" // match
"$5.000" // should not match
"$5000.50" // should not match
因此,基本上,它应该只与确切的用户输入匹配,但美元字符($)除外,如果后面有一个值,但它们之间用加号分隔(但仍是相同的字符串),或者该值包含写入较大数字(例如5,000)时常用的逗号。
RegExp对象可以做到这一点吗?
首先,我们必须稍微准备一下用户输入-删除$
如果存在),并添加正则表达式的开头,以允许在数千个适当位置使用可选逗号。 我已经分解了各个步骤,尽管可以将它们组合在一起:
var userInput = "$5000";
var userInputWithoutDollar = userInput.replace(/^\$/, "");
var userInputWithOptionalCommas = userInputWithoutDollar.replace(/\B(?=(\d{3})+(?!\d))/g, ",?");
最后一点是此答案的一种变体,用于将逗号作为数千个分隔符添加到数字。
然后,我们构建正则表达式,并记住在使用字符串文字构建正则表达式时,我们必须两次转义(因为反斜杠在字符串文字和正则表达式级别上都是有意义的):
var rex = new RegExp("(?:^|[^.,\\d])((?:\\$?)" + userInputWithOptionalCommas + ")(?=$|[^.,\\d])");
说的是:
.
, ,
,或数字 .
, ,
,或数字 匹配的文本是第一个捕获组。
带有测试的示例:
runTests("$5000", [ {result: true, value: "$5000"}, // match {result: true, value: "$5,000"}, // match as it resembles the same value {result: true, value: "$5000 + $500"}, // match {result: true, value: "$500 + $5000"}, // match {result: false, value: "$5.000"}, // should not match {result: false, value: "$5000.50"} // should not match ]); runTests("500", [ {result: true, value: "$500"}, {result: true, value: "$500"}, {result: false, value: "$1,500"}, {result: false, value: "1,500"} ]); function runTests(userInput, tests) { var userInputWithoutDollar = userInput.replace(/^\\$/, ""); var userInputWithOptionalCommas = userInputWithoutDollar.replace(/\\B(?=(\\d{3})+(?!\\d))/g, ",?"); var rex = new RegExp("(?:^|[^.,\\\\d])((?:\\\\$?)" + userInputWithOptionalCommas + ")(?=$|[^.,\\\\d])"); tests.forEach(function(test) { var match = rex.exec(test.value); var result = !!match; console.log( (result == test.result ? "GOOD" : "ERROR") + " - Value '" + test.value + "' " + (result ? "Matches, got '" + match[1] + "'" : "Doesn't match") ); }); }
.as-console-wrapper { max-height: 100% !important; }
除带逗号的示例外,以下正则表达式可满足您的要求:
(?:\\$?)\\$5000(?:$|\\s.*)
请记住逃脱$
在$5000
(?:\\$?)
:匹配单个$
符号(如果存在) ?
表示$
出现为1或0。
\\$5000
:它与输入$
逃脱。
(?:$|\\s.*)
:匹配字符串的结尾或空格之后的任何内容。
同样,不容纳逗号。
(?:\\$?)\\$50(?:$|(?:\\s(?:\\+|\\-)\\s(?:\\$?)(?:[0-9]+))+)
(?:\\$?)
:匹配单个$
符号(如果存在) ?
表示$
出现为1或0。
\\$5000
:它与输入$
逃脱。
(?:$|(?:\\s(?:\\+|\\-)\\s(?:\\$?)(?:[0-9]+))+)
:匹配字符串结尾或+ $xxx
比赛后的+ $xxx
或- $xxx
,即使重复。
将$转换为\\ $并加上,? 到userInput
值,使正则表达式模式
var userInput = "500000000" ;
if(userInput.substring(0,1) != "$"){userInput = "$"+userInput ;}
var c = userInput.length-2;
for(var i = Math.floor(c/3)*3 ; i>0 ; i-=3){
userInput = userInput.substring(0,userInput.length-i) + ",?" + userInput.substr(-i) ;
}
userInput = "(?:^|\\b)" + userInput.replace("$","\\\$\?") + "(?=\\s|$)" ;
为5000,输出为:
\$?5,?000
//updated to
(?:^|\b)\$?5,?000(?=\s|$)
见演示
更新完成:
找到号码后将(?:..)
更改为(?=..)
以停止匹配
错过了用“ \\ $”替换“ $”的分配? 到userInput
var regExp = /^\\$(50*)|(\\+50*)/igm ; console.log(regExp.test("$50")); //true console.log(regExp.test("$50000")); //true console.log(regExp.test("$50+50")); //true console.log(regExp.test("$50+500000")); //true console.log(regExp.test("$50.00")); //false console.log(regExp.test("$50.00+50")); //false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.