[英]How to replace more than once?
This is my code so far: 到目前为止这是我的代码:
$("h1.intro:contains('|')").each(function() {
$(this).html($(this).html().replace('|','</span><br /><span>'))
});
This works just once, but it has to work for all of those "|"... 这只能工作一次,但它必须适用于所有这些“|”......
any ideas? 有任何想法吗?
Add /g
modifier: 添加
/g
修饰符:
$("h1.intro:contains('|')").each(function() {
$(this).html($(this).html().replace(/\|/g, '</span><br /><span>'));
});
More Info: 更多信息:
The g modifier is used to perform a global match (find all matches rather than stopping after the first match).
g修饰符用于执行全局匹配(查找所有匹配项,而不是在第一次匹配后停止)。
If you are using jQuery 1.4, you can do this more nicely using the .html(function))
signature: 如果您使用的是jQuery 1.4,则可以使用
.html(function))
签名更好地完成此操作:
$("h1.intro:contains('|')").each(function() {
$(this).html(function(idx, oldContent) {
return oldContent.replace(/\|/g, '</span><br /><span>');
});
});
This means you don't have to create a second jQuery instance and should perform better. 这意味着您不必创建第二个jQuery实例,并且应该执行得更好。
Hi add a modifier to your regex by adding the "/g" after "|" 您可以通过在“|”之后添加“/ g”为正则表达式添加修饰符
$("h1.intro:contains('|')").each(function() {
$(this).html($(this).html().replace("|/g",'</span><br /><span>'))
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.