Say I have the following:
var strRandomString = "I have 2 apples and 6 oranges and 3 grapes";
Now I want to replace the word "apples" with the word "oranges" and vice-versa. Order is not fixed and replace should be global. This makes the end result:
document.write(strRandomString) \\"I have 2 oranges and 6 apples and 3 grapes";
Currently, the best way I can think to do this is:
strRandomString=strRandomString.replace("apples","*******");
strRandomString=strRandomString.replace("oranges","apples");
strRandomString=strRandomString.replace("*******","oranges");
Is there a way to do this with a single replace?
Say I have the following:
var strRandomString = "I have 2 apples and 6 oranges and 3 grapes";
Now I want to replace the word "apples" with the word "oranges" and vice-versa. Order is not fixed and replace should be global. This makes the end result:
document.write(strRandomString) \\"I have 2 oranges and 6 apples and 3 grapes";
Currently, the best way I can think to do this is:
strRandomString=strRandomString.replace("apples","*******");
strRandomString=strRandomString.replace("oranges","apples");
strRandomString=strRandomString.replace("*******","oranges");
Is there a way to do this with a single replace?
Share Improve this question edited Aug 18, 2014 at 16:08 falsetru 370k66 gold badges765 silver badges658 bronze badges asked Aug 18, 2014 at 15:55 David StarkeyDavid Starkey 1,8403 gold badges36 silver badges50 bronze badges 7- 1 well what effort have you made? – Daniel A. White Commented Aug 18, 2014 at 15:56
- How did you replace the one? – epascarello Commented Aug 18, 2014 at 15:56
- Searching has provided no results, but I may be missing the proper keywords to find anything. – David Starkey Commented Aug 18, 2014 at 15:57
- @DanielA.White I mentioned how it could be acplished, but I've added the specific code to better demonstrate. – David Starkey Commented Aug 18, 2014 at 16:00
- 1 A pure regex solution is not possible, but you can use a callback function to decide which replacement text to use. Would that be acceptable? – Tim Pietzcker Commented Aug 18, 2014 at 16:07
3 Answers
Reset to default 8String.prototype.replace
not only accepts string as a replacement, but also accepts a function. The return value of the function is used as replacement string.
var strRandomString = "I have 2 apples and 6 oranges and 3 grapes";
strRandomString.replace(/apples|oranges/g, function(m) {
// `m` is a matched string.
return m === 'apples' ? 'oranges' : 'apples';
})
// => "I have 2 oranges and 6 apples and 3 grapes"
You can do something with capture groups
var strRandomString = "I have 2 apples and 6 oranges and 3 grapes";
console.log(strRandomString.replace(/(apples|oranges)(.+)(apples|oranges)/g,"$3$2$1"));
you could always makes things a bit more elegant by adding a function on the string's prototype:
String.prototype.switch = function(str1, str2){
var delimiter = '__#$@%__';
return this.replace(str1, delimiter).replace(str2,str1).replace(delimiter,str2);
}
var str = "orange apple switch";
var newStr = str.switch("orange","apple");
console.log(newStr);
Fiddle
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744389439a4571831.html
评论列表(0条)