简体   繁体   中英

regex to match first occruence and everything in between until last match

I may be thinking this about the wrong way.

The first three (...) 's are generated and could be any number. I only want to catch these first set of items and allow the user to use () inside of their custom string.

Test String

(374003) (C6-96738) (WR183186) R1|SALOON|DEFECTIVE|WiFiInfotainment|Hardware detects WIFI but unable to log in on the (JAMIE HUTBER) internet.:

Regex

/\(([^)]+)\)/g

Current output

 ["(374003)", "(C6-96738)", "(WR183186)", "(JAMIE HUTBER)"]

Desired Output

 ["(374003)", "(C6-96738)", "(WR183186)"]

You can use two ways to do that:

  1. get only 3 items from array
  2. add space to your regexp \\(([^ )]+)\\) ( https://regex101.com/r/ZPdq35/1/ )

Using the sticky option /y you can then use regEx's ability to find all occurrences..

This will then work, if there is not a space in JAMIE HUNTER , etc..

eg.

 const re = /\\s*\\(([^)]+)\\)/y; const str = "(374003) (C6-96738) (WR183186) R1|SALOON|DEFECTIVE|WiFiInfotainment|Hardware detects WIFI but unable to log in on the (JAMIE HUTBER) internet.:"; let m = re.exec(str); while (m) { console.log(m[1]); m = re.exec(str); } 

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