提前谢谢。
我在用
$(document).ready(function() {
$("body").html(
$("body").html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>®</sup>')
);
});若要在regmark周围添加下标,但如果标记已经具有< sup >标记,则将添加两次上标。有人能帮我加上一张支票,如果已经有了吗?
update::我最后使用了这段代码,因为下面的答案干扰了我的javascript的其余部分。
$(document).ready(function() {
$("p,h1,h2,h3,h4, td").each(function(){
$(this).html($(this).html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>®
</sup>').replace('<sup><sup>', '<sup>').replace('</sup></sup>', '</sup>'));
});
});发布于 2014-11-25 00:49:28
你想要这个
$("body").html(
$("body").html()
.replace(/((?!<sup>\s*))®((?!\s*<\/sup>))/gi, '<sup>®</sup>') // wrap ® if not wrapped yet
.replace(/((?!<sup>\s*))®((?!\s*<\/sup>))/gi, '<sup>®</sup>') // wrap ® if not wrapped yet
);解释:
您可以在regex本身中为包装标记构建测试,而不是像其他人建议的那样删除副本。
例如,此正则表达式仅在字符(实体)未被SUP标记包围时才会找到:
/((?!<sup>\s*))®((?!\s*<\/sup>))/gi和/((?!<sup>\s*))®((?!\s*<\/sup>))/gi
JsFiddle演示:http://jsfiddle.net/co3sy5zo/
可运行片段:(使用本机javascript而不是jQuery)
document.body.innerHTML =
document.body.innerHTML
.replace(/((?!<sup>\s*))®((?!\s*<\/sup>))/gi, '<sup>®</sup>')
.replace(/((?!<sup>\s*))®((?!\s*<\/sup>))/gi, '<sup>®</sup>');<sup>®</sup>
®
<sup>®</sup>
®
<sup> ® </sup>
<sup> ® </sup>
<sup>
®
</sup>
<sup> ®
</sup>
"Buy more Emo® Jeans!"
发布于 2014-11-24 23:53:50
有更有效的方法来做到这一点,但最简单的方法就是这样做:
$(document).ready(function() {
$("body").html(
$("body").html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>®</sup>').replace('<sup><sup>','<sup>').replace('</sup></sup>','</sup>');
);
});发布于 2014-11-24 23:54:56
添加更多的替换
$("body").html().replace(/®/gi, '<sup>®</sup>').replace(/®/gi, '<sup>®</sup>').replace('<sup><sup>', '<sup>').replace('</sup></sup>', '</sup>')https://stackoverflow.com/questions/27116372
复制相似问题