效果图:

RegExp 正则匹配一句话,其中一个单词显示高亮

<script type="text/javascript">

var str1 = 'For months, city has been engulfed in widespread violence and mobs attacksp the ';

var result1 = reg_replace(str1, "the");

console.log(result1);

function reg_replace(str, keyword) {

var reg = new RegExp(keyword, "gi");

var result = [];

var match_result = str.split(reg);

if (match_result.length > 1) {

for (var i = 0, length = match_result.length; i < length; i++) {

if (i) {

result.push(keyword);

}

result.push(match_result[i]);

}

} else {

match_result.unshift('');

//push('');

match_result.push('');

 

result = match_result;

}

 

return result;

console.log(result)

}

</script>

 

小程序中:

RegExp 正则匹配一句话,其中一个单词显示高亮

相关文章:

  • 2021-11-19
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案