简体   繁体   中英

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.

What i've got so far only matches at least 4 spaces.So it accepts 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)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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