[英]Javascript question regarding the use of regex
I need to find a regex expression for a number 1 to 9, followed by trailing zeroes, followed by the end number 1 to 9. As like in a minesweeper game for clearing zeroes.我需要为数字 1 到 9 找到一个正则表达式,然后是尾随零,最后是数字 1 到 9。就像在扫雷游戏中清除零一样。
How do I match the part of an array where like i have 10009 or 2003 ?我如何匹配数组的一部分,比如我有 10009 或 2003 ? ,1to9 then zeroes, then 1to9?
,1to9 然后归零,然后 1to9? How would I write it?
我该怎么写?
does this work?这行得通吗? updated: how do I ask this regex or another regex?
更新:我如何询问这个正则表达式或另一个正则表达式? the one i have below or a (trailing zeroes and 1-9)
我下面的那个或一个(尾随零和 1-9)
(^[1-9][0+][1-9]$)
[1-9][0]+[1-9]
Move the +
outside of the square brackets.将
+
移到方括号之外。 Otherwise, it will match the literal character.否则,它将匹配文字字符。
const regex = new RegExp("[1-9][0]+[1-9]") function test(testCase){ console.log(regex.test(testCase)) } test("10009") test("2003")
To make the first digit optional, you can do:要使第一个数字可选,您可以执行以下操作:
[1-9]?[0]+[1-9]
const regex = new RegExp("[1-9]?[0]+[1-9]") function test(testCase){ console.log(regex.test(testCase)) } test("0009") test("2003")
When you say [0+]
then you are saying that select any of either 0
or +
当您说
[0+]
您是说选择0
或+
任何一个
you want is quantifier 0+
which means 0 one or more times
你想要的是量词
0+
这意味着0 one or more times
You can use ^[1-9]0+[1-9]$
您可以使用
^[1-9]0+[1-9]$
const regex = /^[1-9]0+[1-9]$/; function test(str) { return regex.test(str); } console.log(test("10009")); console.log(test("2003"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.