简体   繁体   中英

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?

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 ;)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM