Javascript regex - a certain number of characters in an unordered string

I am trying to check if an unordered string "3" in it 5 times.

For instance:

var re = /3{5}/; re.test("333334"); //returns true as expected re.test("334333"); //returns false since there is no chain of 5 3s 

In what regular expression will the second line be returned? If regex is not the best way to test this, what is it?

Thanks!

+5
source share
4 answers

Try

 (str.match(/3/g) || []).length >= 5 

or

 str.split(3).length > 5 

Where str is the string you want to test.

+6
source

You can write this:

 var re = /(?:3[^3]*){5}/; 
+3
source

I would go for

 s.replace(/[^3]/,'').length >= 5 

Assuming the test string is called s

+2
source

I would go with:

 string.indexOf('33333') > -1 
0
source

Source: https://habr.com/ru/post/1202031/


All Articles