Reputation: 8041
This is my string
var str = "Here is some &text:also , here is &another:one"
var myRegExp = /\b\s(\&){1}(\w)+(\:){1}(\w)+\s?\b/g
str.match(myRegExp)
//[" &text:also", " &another:one"]
Works as expected till now, the problem is with the splitting
str.split(myRegExp)
Expectations: ["Here is some" , ", here is"]
Reality ["Here is some", "&", "t", ":", "o", " , here is", "&", "r", ":", "e", ""]
What could be the reason for this ? I can extract my required array by looping over and splitting at the matches, isnt there any shortcut apart from that ?
Upvotes: 0
Views: 185
Reputation: 2852
You don't need the groupings or the {1}
in the regex. Use:
var myRegExp = /\b\s&\w+:\w+\s?\b/g
Upvotes: 1
Reputation: 32787
This is because the regex
engine is also including the group
values into the result..use (?:)
So the regex to split would be
/\b\s(?:\&){1}(?:\w)+(?:\:){1}(?:\w)+\s?\b/g
Or there is no need of grouping it at all
/\b\s\&{1}\w+\:{1}\w+\s?\b/g
Upvotes: 2