Reliable replace function?

D

D. Alvarado

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.replace("_", " ");

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
 
L

Lee

D. Alvarado said:
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.replace("_", " ");

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

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.replace(/_/g, " ");
 
T

Thomas 'PointedEars' Lahn

D. Alvarado said:
var str = requiredField.replace("_", " ");

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

Works as designed.

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

RTFM.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top