Skip to content
Advertisement

combine regex to match subgroups in different order

I have a string with may have one of the 2 below structure

JavaScript

some examples would be

JavaScript

The goal is to match as below

JavaScript

I can manage to get the result with 3 regex, but not with 1

JavaScript

How can I get the expected result with a single regex ?

Advertisement

Answer

Maybe use | to separate possible matches:

JavaScript

Also note that (w+){1} is equivalent to w+, and [!] and [#] are the same as ! and # respectively.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement