x.replace(/old/gi. 'new');
x.replace(/whatever/gi. 'whatevernew');
x.replace(/car/gi. 'boat');
有没有办法将这些内容组合成一个正则表达式语句,然后再加上旧词和新词的数组。PHP解决方案也可以。
x.replace(/old/gi. 'new');
x.replace(/whatever/gi. 'whatevernew');
x.replace(/car/gi. 'boat');
var x = 'The old car is just whatever';
var arr = [{ old: /old/gi, new: 'new' },
{ old: /whatever/gi, new: 'whatevernew' },
{ old: /car/gi, new: 'boat' }];
for (var ii = 0; ii < arr.length; ii++) {
x = x.replace(arr[ii].old, arr[ii].new);
}
x.replace(new RegExp(arr[ii].old, 'gi'), arr[ii].new)
。 - Kobifunction multiReplace(str, params) {
var regStr = "";
for (var i in params) {
regStr += "(" + i + ")|"; // build regEx string
}
regStr = regStr.slice(0, -1); // remove extra trailing |
return(str.replace(new RegExp(regStr, "gi"), function(a) {
return(params[a]);
}));
}
var test = 'This old car is just whatever and really old';
var replaceParam = {"old": "new", "whatever": "something", "car": "boat"};
var result = multiReplace(test, replaceParam);
alert(result);
这里有一个演示它工作的示例: http://jsfiddle.net/jfriend00/p8wKH/
试试这个:
var regexes = { 'new': /old/gi, 'whatevernew': /whatever/gi, 'boat': /car/gi };
$.each(regexes, function(newone, regex){
x = x.replace(regex, newone);
});
或者这样:
var regexes = { 'old':'new', 'whatever':'whatevernew', 'car':'boat'};
$.each(regexes, function(oldone, newone){
x = x.replace(new RegExp(oldone, 'gi'), newone);
});
whatever
和new
可能会在whatevernew
之前被替换,因此您可能会在不同的迭代(或至少不同的浏览器)中获得不同的结果。 - KobijQuery.each()
的顺序的任何资源。你有吗? - Sanghyun Leereplace方法支持使用回调函数:
var x = 'This old car is just whatever';
var y = x.replace(/(old)|(whatever)|(car)/gi,function (a) {
var str = "";
switch(a){
case "old":
str = "new";
break;
case "whatever":
str = "something";
break;
case "car":
str = "boat";
break;
default:
str= "";
}
return str;
});
alert(y);
// Y will print "This new car is just something"
这里还有几个:
// Multi RegExp version
var replaceSeveral = (function() {
var data = {old:'new', whatever: 'whatevernew', car: 'boat'};
return function(s) {
var re;
for (var p in data) {
if (data.hasOwnProperty(p)) {
re = new RegExp('(^|\\b)' + p + '(\\b|$)','ig');
s = s.replace(re, '$1' + data[p] + '$2');
}
}
return s;
}
}());
// Replace function version
var comparitor = (function() {
var data = {old:'new', whatever: 'whatevernew', car: 'boat'};
return function (word) {
return data.hasOwnProperty(word.toLowerCase())? data[word] : word;
}
}());
var s = 'old this old whatever is a car';
alert(
s
+ '\n' + replaceSeveral(s)
+ '\n' + s.replace(/\w+/ig, comparitor)
);
Ibu的回调解决方案非常干净,但可以进一步简化:
x = x.replace(/\b(?:old|whatever|car)\b/gi,
function (m0) {
return {'old': 'new',
'car': 'boat',
'whatever': 'something'}[m0];
});
使用对象文字的技巧非常有效。我修改了正则表达式以匹配完整单词,通过添加单词边界(避免将gold
更改为gnew
等)。
编辑:经过仔细检查,我发现jfriend00的解决方案正在使用相同的技术(并且是通用的,更加有用)。