Using javascript I need to get the text and tokens from string like:
"type":"homePhone","means":"$[createJohnRequest.contactInfo[type=homePhone].means]"
Such that a regex will return:
$[createJohnRequest.contactInfo[type=homePhone].means]
I have a few attempts at this but none that work:
/(\$\[(.*?]))/g
will return: $[createJohnRequest.contactInfo[type=homePhone]
/(\$\[(.*]))/g
This works in the above case but is way too greedy for a case like:
{"firstName":"$[user.firstName]","userName":"$[user.username1]","details":
{"description":"this is $[user.username1] the $[user.username2] text th $[user.username3]
at conta$[user.username4]ins the terms we want to find. $[final.object]"}}
Ideally I want a single regex to match both in multiline text:
some text here $[some.value.here]bunch of noisy text in between here
some more text here$[some.value[index]goes.here]some more noise here
$[some.value.here] and $[some.value[index].goes.here]
Anyone have any ideas to point me in the right direction?
I am leaning toward using $[some token]$ instead which is pretty simple to capture.
You want something like this, as long as the bracket nesting level is limited to two:
/\$\[(\[.*]|.)*?\]/g
In English:
a dollar sign followed by
a bracketed sequence whose content is
any number of occurrences of
either
a bracketed subsequence whose content is anything
or something else
If you want to support deeper nesting, I suggest you write a little function to build the regexp for you, so you don't go insane:
function make_regexp(level) {
var build = "\\[.*?]";
while (level--) {
build = "(\\[" + build + "]|.)*?";
}
return RegExp("\\$" + build, "g");
}
> make_regexp(3)
/\$(\[(\[(\[.*]|.*)*?]|.*)*?]|.*)*?/g