[thelist] Stupid javascript String.replace() question

Iva Koberg iva at livestoryboard.com
Tue Dec 10 00:07:01 CST 2002


You want to use the "g" modifier after your RegExp's pattern to perform
a global search and not stop after the first match.

ex. myString.replace(/foo/g, "bar");

best,

Iva Koberg
www.livestoryboard.com

>> -----Original Message-----
>> From: thelist-admin at lists.evolt.org
>> [mailto:thelist-admin at lists.evolt.org] On Behalf Of Roger Ly
>> Sent: Monday, December 09, 2002 5:30 PM
>> To: thelist at lists.evolt.org
>> Subject: [thelist] Stupid javascript String.replace() question
>>
>>
>> --
>> [ Picked text/plain from multipart/alternative ]
>> My O'Reilly says String.replace(regexpression , replaceString)
>>
>> Returns the new string that results when all matches of the
>> regexpression parameter are replaced by the replaceString parameter.
>>
>> Why doesn't this work for me?
>>
>>       function truncateCreditCard(ccNumber)
>>       {
>>             alert(ccNumber.replace(/\D+/, ''));
>>             alert(ccNumber.replace(/\D*/, ''));
>>             alert(ccNumber.replace(/[^0-9]*/, ''));
>>             alert(ccNumber.replace(/[^0-9]+/, ''));
>>       }
>>
>> I want to strip out all non-numeric characters from a string
>> that I am
>> passing in.  Want to allow a string like "1111 2222 3333 4444" return
>> "1111222233334444".  When I execute
>> truncateCreditCard("1111 2222 3333 4444"); I get four alerts that all
>> say:
>>
>> 11112222 3333 4444
>>
>> where the first space is removed, but the others remain.
>>
>> Sorry, maybe I've been up too long, but am I missing something stupid
>> here?
>>
>> Roger
>>
>> --
>>
>> --
>> * * Please support the community that supports you.  * *
>> http://evolt.org/help_support_evolt/
>>
>> For unsubscribe and other options, including the Tip Harvester
>> and archives of thelist go to: http://lists.evolt.org
>> Workers of the Web, evolt !
>>





More information about the thelist mailing list