JavaScript替换字符串中的特定单词

时间:2018-08-08 09:09:02

标签: javascript regex

如何替换字符串中的特定单词? 如果要在此字符串中将SignInStatus result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 替换为["hi", "hello", "welcome"]。 (不区分大小写)

因此greetings将变成Hi there Welcome hi

1 个答案:

答案 0 :(得分:0)

按照示例

String.prototype.allReplace = function(obj) {
    var retStr = this;
    for (var x in obj) {
        retStr = retStr.replace(new RegExp(x, 'g'), obj[x]);
    }
    return retStr;
};
var v = 'aabbaabbcc'.allReplace({'a': 'h', 'b': 'o'});