[thelist] JavaScript snafu (trying to replace characters in astring)

Sam-I-Am sam at sam-i-am.com
Wed Aug 1 13:35:06 CDT 2001


as .jeff points out there are other better ways to acheive what you
want, but so you don't pull your hair out next time: 

>     var cgname = replace(name,'_',' ') // here is where I tried to replace _ with a 

replace is a string method. That means it needs a string to operate on,
so it should look something more like 

var cgname = name.replace('_',' ')

(aside: but I'd not use 'name' as the name of a variable.. it looks too
much like a property. Maybe sName)

the first argument can be a regular expression, so you could do
re = /_/g
var cgname = name.replace(re,' ')

.. to catch all _ characters in your string. 

Sam




More information about the thelist mailing list