[英]Javascript regex - specific number of characters in unordered string
I'm trying to test whether or not an unordered string has '3' in it 5 times. 我正在尝试测试无序字符串中是否有5次“3”。
For example: 例如:
var re = /3{5}/;
re.test("333334"); //returns true as expected
re.test("334333"); //returns false since there is no chain of 5 3s
What regex would make the second line return true? 什么正则表达式会使第二行返回true? If regex is not the best way to test this, what is? 如果正则表达式不是测试它的最佳方法,那是什么?
Thanks! 谢谢!
Try 尝试
(str.match(/3/g) || []).length >= 5
Or 要么
str.split(3).length > 5
Where str
is the string you want to test. 其中str
是您要测试的字符串。
你可以这样写:
var re = /(?:3[^3]*){5}/;
I would go for 我会去的
s.replace(/[^3]/,'').length >= 5
Assuming that the string to be tested is named s
假设要测试的字符串名为s
我会选择:
string.indexOf('33333') > -1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.