JS regex match all words in a string

Actually, I work in a language support for Atom (by git) and I use js regex but I must learn more about that. I need to capture on groups:

/exe c:\dos\main.cpc /l:check

I need a group with exe/, another with c:\dos\main.cpc, another with /l: and the last with check.

I already tried that:

(exe/)([^=]+)(/l:)(.*)

but it did not work.

Can you help me?

Upvotes: 1

Views: 632

Answers (2)

trincot
trincot

Reputation: 350137

You could use this one:

(.*?)\s+(.*?)\s+(.*?:)(.*)

Here is a regex test example of that.

The logic is as follows:

  • first group selects all characters up until the first blank (excluded)
  • any number of white space characters can follow, but are not captured
  • second group selects all characters up until the next blank (excluded)
  • any number of white space characters can follow, but are not captured
  • third group selects all characters up until, and including, a colon
  • fourth group selects all characters that follow.

Upvotes: 0

Long Nguyen
Long Nguyen

Reputation: 11275

You forgot to escape delimiter /

Your regex should be:

(exe\/)([^=]+)(\/l:)(.*)

I suggest you test your regex on this website: https://regex101.com/

It will check the syntax for you with the explanation of every elements you use in your regex ;)

Upvotes: 3

Related Questions