Regular expression for a specific pair or each item in a pair

I have a situation where I can get one or both pairs of characters, and I also want to match.

For instance:

str = 'cddd a dfsdf b sdfg ab uyeroi'

I want to match any "a" or "b" or "ab". If "ab" gets together, I want to catch it as one match (not as two matches "a" "b"). If I get "ab", it will always be in that order ("a" will always precede "b")

I have:

/[ab]|ab/

But I'm not sure what abwill be a stronger match than [ab].

Thanks for the help.

+3
source share
3 answers

, , - a b. regex:

, , , , . / , , . , regex regex|regex not regex not. regex, . regex not, . , , , "".

, , , , ab:

/ab|[ab]/

, :

/ab?|b/
+6

:

((ab)|a|b)

(ab|[ab])
0

You have instances of a, b, and ab as words. Do you need to find them only as whole words? If so, you should try

/ \ woman \ b | \ b [ab] \ b /

0
source

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


All Articles