javascript - Replace X with Y and Y with X in a single Regular Expression - Stack Overflow

Say I have the following:var strRandomString = "I have 2 apples and 6 oranges and 3 grapes";N

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
 |  Show 2 more ments

3 Answers 3

Reset to default 8

String.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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信