JavaScript/replace:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第30行: 第30行:
</pre>}}}}
</pre>}}}}
替换内容。
替换内容。
<syntaxhighlight lang="javascript">
{{leftrightflex|<syntaxhighlight lang="javascript">
_.name.replace(new RegExp(searchValue, 'gi'), function(targetKey, targetIndex, content) {
const foo = 'abckangjiandefghkangjianijk';
  return content.substring(0, targetIndex) + '<span>' + targetKey + '</span>' + content.substring(targetIndex + 1);
const bar = 'kangjian';
});</syntaxhighlight>
const baz = foo.replace(
  new RegExp(bar, 'gi'),
  function(targetKey, targetIndex, content) {
    return '<span>' + targetKey +'</span>';
});
console.log(baz);</syntaxhighlight>|{{consoleout|<pre>
abc<span>kangjian</span>defgh<span>kangjian</span>ijk
</pre>}}}}