Jquery 操纵变量以降低空格并用“替换”-&引用;

Jquery 操纵变量以降低空格并用“替换”-&引用;,jquery,Jquery,我有一些代码来替换###body inner中我的页面内容中的##some Content##的任何实例,并将其替换为: <span class="tooltipster" data-tooltip-content="#Some Content">Some Content</span> 您可以使用回调函数来执行此操作。尽管您可以通过对方法使用回调来避免方法 //使用带有回调的html方法,该方法在第二个参数为旧html的元素上迭代 $('#body inner').ch

我有一些代码来替换###body inner中我的页面内容中的##some Content##的任何实例,并将其替换为:

<span class="tooltipster" data-tooltip-content="#Some Content">Some Content</span>
您可以使用回调函数来执行此操作。尽管您可以通过对方法使用回调来避免方法

//使用带有回调的html方法,该方法在第二个参数为旧html的元素上迭代
$('#body inner').children().html(函数(i,html){
//替换所有匹配子字符串
返回html.replace(/###(.*)##/gm,函数(m,m1){
//生成html字符串
返回“+m1+”;
//您还可以使用.replace(/\s/g,'-'),而不是拆分和联接
});
});
$('#body-inner').children().html(函数(i,html){
返回html.replace(/###(.*)##/gm,函数(m,m1){
返回“+m1+”;
});
});

##abc def##
abc##def dfd#####fdf sds##

谢谢。这正是我想要的。
$('#body-inner').children().each(function(){
        $(this).html( 
            $(this).html().replace(/##(.*?)##/gm,'<span class="tooltipster" data-tooltip-content="#$1">$1</span>')
        );
    });
// use html method with callback which iterates over the element where old html is the second argument
$('#body-inner').children().html(function(i, html) { 
  // replace all match substring
  return html.replace(/##(.*?)##/gm, function(m, m1) {
    // generate the html string
    return '<span class="tooltipster" data-tooltip-content="#' + m1.toLowerCase().split(' ').join('-') + '">' + m1 + '</span>';
    // instead of split and join, you can also use .replace(/\s/g, '-')
  });
});