Yash Damani
Yash Damani

Reputation: 125

How do I select a minimum occurrence of the characters I want to select?

For example, the characters c,a,t should be selected from a string such that at least one occurrence of each character has happened. My regex : /[o{1,}w{1,}l{1,}]/gmi I cannot get it to select a minimum occurrence of at least 1.

kkcnjnkannt //true as c,a and t are there at least once.
kkjcsnknna //false as c and a are there but not t.

Upvotes: 0

Views: 62

Answers (1)

The fourth bird
The fourth bird

Reputation: 163207

Your regex [o{1,}w{1,}l{1,}] consists of a character class which is the same as [o1,wl{}]

To check if there is a c, a and t in your string, you could use 3 times a positive lookahead.

^(?=.*c)^(?=.*a)^(?=.*t).*$

Regex demo

Upvotes: 2

Related Questions