这是困扰我一段时间的事情。
假设我在一条线上这样做
var result = "Noble warm and pretty darm Caesar.".split(/(\warm)/);
// ["Noble ", "warm", " and pretty ", "darm", " Caesar."]
是否可以扩展 split 方法以使用类似replace
字符串的函数来操作正则表达式捕获?
伪代码:
var result = "Noble warm and pretty darm Caesar.".split(/(\warm)/, function (match) {
return '<span style="color:red;">' + match + '</span>';
});
// ["Noble ", "<span style=\"color:red;\">warm</span>", " and pretty ", "<span style=\"color:red;\">darm</span>", " Caesar."]