Reliable replace function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Alvarado

    Reliable replace function?

    Hello,
    Can anyone recommend a good Javascript cross-browser string replace
    method? On both PC Mozilla Firefox and IE 6, I tried this

    var str = requiredField.r eplace("_", " ");

    where requiredField = "Ship_To_First_ Name", but sadly the value of str
    was "Ship To_First_Name". It seemed only one "_" had been replaced.

    Thanks for any advice, - Dave
  • Lee

    #2
    Re: Reliable replace function?

    D. Alvarado said:[color=blue]
    >
    >Hello,
    > Can anyone recommend a good Javascript cross-browser string replace
    >method? On both PC Mozilla Firefox and IE 6, I tried this
    >
    >var str = requiredField.r eplace("_", " ");
    >
    >where requiredField = "Ship_To_First_ Name", but sadly the value of str
    >was "Ship To_First_Name". It seemed only one "_" had been replaced.[/color]

    You used a correct and reliable method, you simply failed to read the
    documentation. If you pass a RegExp object as the first argument, you
    can specify the "global replacement" switch:

    var str = requiredField.r eplace(/_/g, " ");

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Reliable replace function?

      D. Alvarado wrote:[color=blue]
      > var str = requiredField.r eplace("_", " ");
      >
      > where requiredField = "Ship_To_First_ Name", but sadly the value of str
      > was "Ship To_First_Name". It seemed only one "_" had been replaced.[/color]

      Works as designed.

      var str = requiredField.r eplace(/_/g, " ");

      RTFM.


      PointedEars

      Comment

      Working...