I want to use JavaScript’s regular expression something like this
/marker\d+"?(\w+)"?\s/gi
In a string like this:
IDoHaveMarker1"apple" IDoAlsoHaveAMarker352pear LastPointMakingmarker3134"foo"
And I want it to return an array like this:
[ "apple", "pear", "foo" ]
The quotes are to make clear they are strings. They shouldn’t be in the result.
If you are asking about how to actually use the regex:
To get all captures of multiple (global) matches you have to use a loop and
execin JavaScript:(Note that you can omit the trailing
"?\s?if you are only interested in the capture, since they are optional anyway, so they don’t affect the matched result.)And no,
gwill not allow you to do all of that in one call. If you had omittedgthenexecwould return the same match every time.As Blender mentioned, if you want to rule out things like
Marker13"something Marker14bar(unmatched") you need to use another capturing group and a backreference. Note that this will push your desired capture to index2: