假如原字符串为

<class="dark" >

要将最后一个_1替换为_3

1、先使用RegexBuddy调试如下

正则替换字符串中的一部分

 
$1,$2代表第1,2个分组,正则表达式()代表分组

 2、手写转换js函数如下

//替换第三个_id
function replaceById(p_html,p_id) {
    var regexp = new RegExp(/(\s>g);
    var newHtml = p_html.replace(regexp, "$1_" + "$2_" + p_id+"\"");// 用正则表达式替换第三个数字
    return newHtml;
}

 g代表全部替换

相关文章:

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