简体   繁体   English

正则表达式以n的倍数匹配

[英]Regex Match In Multiple Of n

I want to match a string that starts with at least 4 spaces and in multiples of 4. So 4, 8, 12. 我想匹配一个以至少4个空格和4的倍数开头的字符串。所以4、8、12。

What i've got so far only matches at least 4 spaces.So it accepts 4, 5, 6... 到目前为止,我只匹配至少4个空格。所以它接受4,5,6 ...

/^[\s]{4,}\+/

Put the pattern which matches exactly four spaces inside a group and then make that group to repeat one or more times. 将与四个空格完全匹配的模式放在一个组中,然后使该组重复一次或多次。 And also you need to add a negative lookahead at the end, so that it won't match the string which has four spaces at the start followed by another space. 另外,您还需要在末尾添加负前行,以使其与开头有四个空格后跟另一个空格的字符串不匹配。

/^(?:\s{4})+(?! )/

DEMO 演示

 var s = " I have four spaces" var s1 = " I have five spaces" alert(/^(?:\\s{4})+(?! )/.test(s)) alert(/^(?:\\s{4})+(?! )/.test(s1)) 

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

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