How to replace all capture groups if it is generated dynamically?
Consider the following code:
function Matcher(source, search) {
var opts = search.split('');
for (var i = 0; i < opts.length; i++) {
opts[i] = '(' + opts[i] + ')';
}
opts = opts.join('.*');
var regexp = new RegExp(opts, 'gi');
source = source.replace(regexp, function () {
console.log(arguments);
return arguments[1];
});
return source;
}
You call the function passing the source as the first parameter and what
you need to match as the second one.
What i need is to replace all capture groups with a bold tag around the
coincidence.
As an example, consider the following:
var patt =
/m([a-z0-9\-_]*?)r([a-z0-9\-_]*?)i([a-z0-9\-_]*?)e([a-z0-9\-_]*\.[a-z]+)/gi;
var newFileName = fileName.replace(patt,
"<strong>m</strong>$1<strong>r</strong>$2<strong>i</strong>$3<strong>e</strong>$4");
This code is a response from Terry (thanks by the way) but the problem
here is that you need to know exactly what you want to replace, and i need
it dynamically.
Any thoughts?
No comments:
Post a Comment