I have some code that searches through text for match, then when it finds a match, performs a non-trivial transformation on the matched text, which I then want to replace with the result of this transformation. For example (ignoring Zalgo issues):
var text = "foo bar <sum>(10+33)/pi</sum> baz";
var matches = text.match(/<sum>[\s\S]+?<\/sum>/g);
matches.forEach(element => {
var result = evaluateSum(element);
// Now somehow replace element with result
});
I could cobble together a hack with RegExp.exec()
, but there must be a neat way of doing this task.