js中RegEx中的匹配模式
Posted
技术标签:
【中文标题】js中RegEx中的匹配模式【英文标题】:match pattern in RegEx in js 【发布时间】:2020-11-07 19:37:09 【问题描述】:我想编辑一个 pattern 来检查文本是否以 number/s 开头并以 number/s 或 operator 结尾 并根据 test() 方法返回 true 或 false。
如何使用 JavaScript 做到这一点?`
var str= "123+125",str2 = "123",str3 = "123*";
let RegularExpression = /^\d*(\d*|(\+|\-|\*|\/)1,)$/; //this pattern variable what I want to edit because it doesn't do what I want.
let result1 = RegularExpression.test(str);
let result2 = RegularExpression.test(str);
let result2 = RegularExpression.test(str);
【问题讨论】:
欢迎来到 ***。目前还不清楚您希望您的正则表达式匹配什么,以及您如何测试它,例如您没有使用str2
和str3
变量,而是绑定了result2
两次。或许可以考虑查看一些关于 JS 中正则表达式的文档,参见例如developer.mozilla.org/en-US/docs/Web/javascript/Reference/…
【参考方案1】:
使用
/^\d+([-+*\/]\d+)*[-+*\/]?$/
见proof
解释:
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[-+*\/] any character of: '-', '+', '*', '\/'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)* end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
[-+*\/]? any character of: '-', '+', '*', '\/'
(optional (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
JavaScript:
var str= "123+125",str2 = "123",str3 = "123*";
let RegularExpression = /^\d+([-+*\/]\d+)*[-+*\/]?$/;
console.log( RegularExpression.test(str) );
console.log( RegularExpression.test(str2) );
console.log( RegularExpression.test(str3) );
【讨论】:
【参考方案2】:您可以使用重复 1 次或多次匹配 1 个或多个数字,可选地后跟一个运算符。
^(?:\d+[+*/-]?)+
说明
^
字符串开始
(?:
非捕获组
\d+[+*/-]?
匹配 1+ 位可选地后跟 +
*
/
-
)+
关闭非捕获组并重复1次以上
Regex demo
let pattern = /^(?:\d+[+*/-]?)+$/m;
[
"123+125",
"",
"123",
"123*",
"123+125-1"
].forEach(s => console.log(s + " --> " + pattern.test(s)));
【讨论】:
【参考方案3】:试试这个模式/^(\d*|[+/*-]?)1,$/
var str= "123+125",str2 = "123",str3 = "123*";
let RegularExpression = /^(\d*|[+/*-]?)1,$/; //this pattern variable what I want to edit because it doesn't do what I want.
let result1 = RegularExpression.test(str);
let result2 = RegularExpression.test(str);
let result3 = RegularExpression.test(str);
console.log("res1:",result1,",res3:",result2,",res3:",result3)
【讨论】:
以上是关于js中RegEx中的匹配模式的主要内容,如果未能解决你的问题,请参考以下文章
在R中,我如何用regex逐行比较两列中的模式行和不匹配行?