combine regex to match subgroups in different order


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

(w+){1}([!][a-zA-Z0-9>,]+)*([#]d+)?

(w+){1}([#]d+)?([!][a-zA-Z0-9>,]+)*

some examples would be

"Abc1!deF2>Ghi3,4jlmNO!pQr5st#1400"
"Abc1#1400!deF2>Ghi3,4jlmNO!pQr5st"

The goal is to match as below

["Abc1", "!deF2>Ghi3,4jlmNO", "!pQr5st", "#1400"]
["Abc1", "#1400", "!deF2>Ghi3,4jlmNO", "!pQr5st"]

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

const a = str.match(/w+/)
const b = str.match(/([!][a-zA-Z0-9>,]+)/g)
const c = str.match(/[#]d+/)

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

Source: JavaSript – Stack Overflow

November 27, 2021
Category : News
Tags: javascript | regex

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.