I have this string:
'newProductsInfo: [[1] - $2 dollars,[2] New Product,[3] Hello,[4]Same, [5]Value]'
The word 'newProductsInfo'
, and a space ' '
always precede the array of strings. I want to be able to return
[1] - $2 dollars
[2] New Product
[3] Hello
[4]Same, [5]Value //should be returned since the comma is followed by a space ' '
in the Regex101 site.
Currently, using (?<=newProductsInfo: [)[^,]]+
only returns [1
.
Edit: added possible return types in bubble plugin creator:
2
Answers
Your pattern (was tagged JavaScript) only matches
[1
because the negated character class[^,]]+
can not cross matching a comma or]
If you want the matches only, you can assert
newProductsInfo:
to the left followed by any characterStart the match with digits between square brackets up until either the next occurrence preceded by a comma, or the end of the string.
Using a lookbehind assertion (see the support) for it:
Regex demo
Edit
If you want to use PHP:
Explanation
(?:
Non capture group for the alternativesnewProductsInfo: [
MatchnewProductsInfo: [
|
OrG(?!^)
Assert the current position at the end of the previous match, not at the start)
Close the non capture group,?K
Match an optional comma and forget what is matched so far[^,n]+
(?>,h+[^,n]+)*
Optionally repeat matching a comma, 1+ spaces and 1+ chars other than,
or newlineRegex demo
Here is one approach using
match()
:The call to
replace()
strips off the leading/trailing[
and]
. The call tosplit()
splits the input string at every comma which is followed by a numbered bracket term such as[2]
.