hi i need to check the validity of all groups and the mixed possibility of creating a world.
for example:
asd
a s d
4 s d
444ssddd
aaaaa444ssss dddd
must all match.
while:
aesd
zxaxvd
anything that have not that pattern
i’ve try with something like this:
(a*|4*|4* *|a* *)(s*|s* *)(d*|d* *)
but this always return true, while should match the entire pattern group:
const regex = '(a*|4*|4* *|a* *)(s*|s* *)(d*|d* *)';
const re = new RegExp(regex, "gi");
const deleteMessage = re.test(text);
3
Answers
The following should work:
In your regexp you used the
*
quantifier which also allows zero occurrences, while in my pattern I require at least one occurrence of the lettersa
or4
,s
andd
.Your pattern has no bounds and doesn’t actually require anything because it uses asterisks for everything, so even an empty string is valid. This should do the trick by adding start and end bounds as well as requiring at least one character from each grouping.
There are two issues with your regex:
You can fix that by using this regex:
which matches:
^
: beginning of string(?:a*4+|a+4*)
: 0 or morea
followed by 1 or more4
; or 1 or morea
followed by 0 or more4
*
: some number of spacess+
: 1 or mores
*
: some number of spacesd+
: 1 or mored
*
: some number of spaces$
: end of stringRegex demo on regex101