Using a function as the second parameter resulted in the desired effect:
var selector = ("'$greetings' '$friend'").replace(/\$([^']+)/g, expand)
function expand(match, offset, fullstring)
{
var dict =
{
...
};
...
}
Which can be generalized into a tokenizer, such as the following:
/* Get all anchors with the javascript: URI scheme */
$("a[href*='javascript']").each(function () {
javascriptURL(arguments[1])
})
/* Replace the javascript: URI with the URL within it */
function javascriptURL(anchor) {
var hyperlink = $(anchor).attr("href").replace(/./g, replacer).substring(1).replace(/'/g, "");
/* Class name for new window binding */
var newWindow = "extWin";
$(anchor).attr({
"href": hyperlink,
"class": newWindow
});
}
/* Grab all text between window.open() parens */
function replacer(match, offset, fullstring) {
var tokens = {
"(": true,
",": false,
";": false
};
/* Consume everything after the left paren */
if (tokens[match] === true) {
replacer.state = true
}
/* Discard everything after the first comma; also reset after a semicolon */
if (tokens[match] === false) {
replacer.state = false
}
/* Continue consuming or discarding otherwise */
if (tokens[match] === undefined) {
replacer.state = replacer.state;
}
/* Return the consumed string or an empty string depending on the tokenizer state */
if (replacer.state === true) {
return match
}
else {
return "";
}
}
function replace(pattern:*, repl:Object):String
repl:Object — Typically, the string that is inserted in place of the matching content. However, you can also specify a function as this parameter. If you specify a function, the string returned by the function is inserted in place of the matching content.
References