Reputation: 842
having a string similar to the following I would like to get the value (test1, test2) after the string -option
(or wathever):
command --option test1 other stuff --option test2 other stuff
I tried with as following
const regex = /--option ([^\w\s]) /g
regex.exec(string)
the problem is that I get just the first occurance and if there is more than 1 space it doesn't get the value
Upvotes: 2
Views: 38
Reputation: 627082
You can use
/--option\s*(.*?)(?=\s*--option|$)/gi
See the regex demo. Details:
--option
- a literal string\s*
- 0 or more whitespaces(.*?)
- Group 1: any zero or more chars other than line break chars, as few as possible(?=\s*--option|$)
- immediately followed with 0+ whitespaces and --option
or end of string.See JavaScript demo:
const str = 'command --option test1 other stuff --option test2 other stuff';
const reg = /--option\s*(.*?)(?=\s*--option|$)/gi;
const results = [...str.matchAll(reg)];
console.log( Array.from(results, x => x[1]) );
Upvotes: 1