Replace array of words

x.replace(/old/gi. 'new'); x.replace(/whatever/gi. 'whatevernew'); x.replace(/car/gi. 'boat'); 

Is there a way to combine them in a single regexp expression and an array of old and new words. A PHP solution is also welcome.

+4
source share
6 answers

Try the following:

 var regexes = { 'new': /old/gi, 'whatevernew': /whatever/gi, 'boat': /car/gi }; $.each(regexes, function(newone, regex){ x = x.replace(regex, newone); }); 

Or that:

 var regexes = { 'old':'new', 'whatever':'whatevernew', 'car':'boat'}; $.each(regexes, function(oldone, newone){ x = x.replace(new RegExp(oldone, 'gi'), newone); }); 
+1
source

You can do something like this:

 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); } 
+2
source

lbu and RobG present an interesting callback approach. Here's a slightly more versatile version of where you have a function that simply takes the data structure of what you want to replace, and that you want to replace it as a parameter.

 function 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); 

And a fiddle that shows this in action: http://jsfiddle.net/jfriend00/p8wKH/

+2
source

The replacement method supports the use of the callback function:

 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" 
+1
source

Here are a couple more:

 // 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) ); 
+1
source

The Ibu callback solution is pretty clean, but can be further optimized:

 x = x.replace(/\b(?:old|whatever|car)\b/gi, function (m0) { return {'old': 'new', 'car': 'boat', 'whatever': 'something'}[m0]; }); 

This method of using an object literal is quite effective. I changed the regular expression to match whole words, adding word boundaries (so as not to change gold to gnew , etc.).

EDIT: on closer inspection, I see that the jfriend00 solution uses the same technique (and generalizes to be more useful).

+1
source

All Articles