用此替换这个或那个

Bra*_*roy 0 jquery replace

所以,假设您想要替换某些部分的href a's:

$("a").each(function() {
    var link = $(this).attr("href").replace("this", "that");
    $(this).attr("href", link);
});
Run Code Online (Sandbox Code Playgroud)

但是你怎么用相同的方法替换字符串的多个部分呢?比方说,你要替换的所有occurence thisthatwhat.

你怎么能最有效地做到这一点?

Ant*_*ton 5

试试这个

 var link = $(this).attr("href").replace(/(this|that)/g, 'what');
Run Code Online (Sandbox Code Playgroud)

结果示例

var str = "test this and that with what"
str.replace(/(this|that)/g, 'what'); //result "test what and what with what"
Run Code Online (Sandbox Code Playgroud)