I have a string something like [[user.system.first_name]][[user.custom.luid]] blah blah
I want to match user.system.first_name
and user.custom.luid
I built /\[\[(\S+)\]\]/
but it is matching user.system.first_name]][[user.custom.luid
.
Any idea where I am doing wrong?
Make it non-greedy as
/\[\[(\S+?)\]\]/
Regex Demo
Make it non-greedy using ?
to match as few input characters as possible. That your regex will be /\[\[(\S+?)\]\]/
var str = '[[user.system.first_name]][[user.custom.luid]] blah blah'
var reg = /\[\[(\S+?)\]\]/g,
match, res = [];
while (match = reg.exec(str))
res.push(match[1]);
document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');
If you need 2 separate matches use:
\[\[([^\]]*)\]\]
Regex101 Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With