Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: replace()

  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    388
    Thanks
    73
    Thanked 0 Times in 0 Posts

    replace()

    I'm using the below code to replace all commas with pipes. It's only replacing the first comma with a pipe and ignoring all the rest. What am I doing wrong??


    Code:
    var text = info.selectionText.replace(",","|");

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    What am I doing wrong??
    expecting String.replace() to do things it doesn’t do.

    by default, replace() will only replace the first occurrence.

    From MDN:
    To perform a global search and replace, either include the g switch in the regular expression or if the first parameter is a string, include g in the flags parameter.
    (note, the flags parameter is non-standard, so it may only work in Mozilla browsers)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    nikko50 (09-26-2013)

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by nikko50 View Post
    It's only replacing the first comma with a pipe and ignoring all the rest. What am I doing wrong??
    For one, you're supplying a string for the first argument instead of a regular expression. This is allowed because of automatic type conversion, but bad practice IMO.

    For two, you don't have the global flag set that tells the regular expression to replace every occurrence of the match. You can't specify that flag when you supply a string as the first argument.

    Correct example:
    Code:
    var text = ",,,";
    text.replace(/,/g, "|");
    // Result: "|||"
    Or if you need to use a string to create the expression because your expression needs to be dynamic:
    Code:
    var text = ",,,";
    var dynamicallyGeneratedSymbol = ",";
    text.replace(RegExp(dynamicallyGeneratedSymbol, "g"), "|");
    // Result: "|||"
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    nikko50 (09-26-2013)

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    a very simple and fast-performing way to do it is to use strings instead of regexp, but using split()/join() instead of replace() to replaceAll:

    Code:
    var text = info.selectionText.split(",").join("|");
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by rnd me View Post
    a very simple and fast-performing way to do it is to use strings instead of regexp, but using split()/join() instead of replace() to replaceAll:

    Code:
    var text = info.selectionText.split(",").join("|");
    Based on this js perf, using replace is way much faster than split+join.

    http://jsperf.com/replace-vs-split-and-join

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by glenngv View Post
    Based on this js perf, using replace is way much faster than split+join.
    i don't know about "way faster", but faster sure. i was contrasting using RegExp() to turn a string into an regexp object, which will very much slow down a routine, sorry if that was less-than clear.

    good info btw.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by glenngv View Post
    Based on this js perf, using replace is way much faster than split+join.
    Quote Originally Posted by rnd me View Post
    i don't know about "way faster", but faster sure. i was contrasting using RegExp() to turn a string into an regexp object, which will very much slow down a routine, sorry if that was less-than clear.
    It depends.

    Based on a revised test at http://jsperf.com/replace-vs-split-and-join/7, in terms of speed, regular expression literals win out in Firefox 24 and IE11 Dev Preview, but are slower than split-join in Chrome 29/Opera 16.

    For dynamic replacement, split-join wins in Firefox and Chrome/Opera, but in IE, the regular expression constructor is faster.

    Just for interest, I also tested using a string as the first argument in the replace method invocation with a loop to complete the replacement, and it significantly slows down the code.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •