Help Regex, ECMAScript

I would like to match a string "abc" or other numbers, and i tried this regex: /[(?:abc)123]/i
but it doesn’t seem to work as expected, it matches only "a" in "abc" but i want to match the whole string "abc".

when you use the square parenthesis you are creating a character class, it will match any character from inside it

a tool like regex101.com can be really helpful in creating regex patterns:

1 Like

yea but i have a non-capturing group inside, why only "a" gets chosen? and how can i match the whole string "abc"?

(abc)|[123] this seem to work?

1 Like

that is not a non capturing group, even if those characters in that order usually have that meaning, put inside the square parenthesis, they are just part of the list of characters matched by the character class, also a charcter class match one single character, if you want consecutive characters matchet you need to use a counter

to match abc just use /abc/

1 Like

Thanks, got it now, so it was considering ( and ) just like any other character :+1:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.