我目前正在重复执行以下任务:
var parts = "<h1>I</h1><p>am a cow;</p>cows say moo. MOOOOO."
.split(/(\bmoo+\b)/gi);
for (var i = 1; i < parts.length; i += 2) {
parts[i] = renderToString(<MyComponent key={i}>{parts[i]}</MyComponent>);
}
var anotherParts = parts.split(/(\bcow+\b)/gi);
for (var i = 1; i < anotherParts.length; i += 2) {
anotherParts[i] = renderToString(<MyOtherComponent key={i}>
{anotherParts[i]}
</MyOtherComponent>)
}
return anotherParts
是否可以立即执行上述任务?我正在寻找最可行的方法,因为我想替换越来越多的字符串。