|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
javascript string manipulation (modifying chars inside a string)
This is simple, but I just forget and it's hard to search it... How do you modify a chars inside a string? For example, convert "_" to " " (spaces.)
__________________
Matthew Doucette / Xona.com |
|
#2
|
||||
|
||||
|
You can use String.replace(pattern,string)
__________________
Cheers, Dave |
|
#3
|
|||
|
|||
|
Thanks, that worked. Here's the code:
"originalstr" is an array of strings. "modifiedstr" is the modified string, modified once for each element in the "originalstr" array. The code: re=RegExp('_','gi'); modifiedstr=originalstr[i].replace(re,' '); |
|
#4
|
|||
|
|||
|
For me, the code did not work. But this one did:
re = new RegExp('_','gi'); modifiedstr = originalstr[i].replace(re,' '); To make it complete, the following replace function will replace any string by any string inside a string: Code:
<html>
<head>
<script>
function replace(myString, toReplace, replacedBy) {
return(myString.replace(new RegExp(toReplace, 'gi'), replacedBy));
}
</script>
</head>
<body onLoad="alert(replace('blablabla', 'bl', 'c'));">
</body>
</html>
Last edited by Birdy : September 9th, 2003 at 07:36 AM. |
|
#5
|
|||
|
|||
|
What browser are you using? Look like you had to add "new" in front of "RegExp"... why is that? Is it possible my code is not working in some browsers?!
Last edited by Doucette : September 9th, 2003 at 09:23 AM. |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > javascript string manipulation (modifying chars inside a string) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|