View Full Version : Replacing Dashes in a String With Spaces

Aug 30th, 2005, 11:26 PM
Hey All,

I've seen this before and know it's simple, but I can't seem to find it anywhere .. I'm trying to grab a string and replace any "-" or "_" values in it with a space.

It's something like varName= varName.replace(dada " "); but I'm not getting it.

Bonus question: If I have array values that look like "2003|name", what's the best way to grab the "2003" and "name" separately?


Aug 30th, 2005, 11:38 PM
I was able to get the replace part working with:

varName= varName.replace(/_/g, " ");

Any suggestions on the latter questions?


Aug 30th, 2005, 11:48 PM
Hi there kraftomatic,

do you mean like this...

<script type="text/javascript">
var str="2003|name"
var part=str.split("|");


Aug 31st, 2005, 12:11 AM
Thanks that helps .. The data I have looks like this:

new Array( "2003|name", "data" )), ..

So it breaks it up, but I almost just need it to check the first part (before the comma) of the array ..

Willy Duitt
Aug 31st, 2005, 12:35 AM
Thanks that helps .. The data I have looks like this:

new Array( "2003|name", "data" )), ..

So it breaks it up, but I almost just need it to check the first part (before the comma) of the array ..

Then split at the comma then split again as above...
Or alternately, you can provide everything we need to know rather than spoonfeed a little information at a time and perhaps someone could write you a regular expression... However, at this point you keep moving the goal post...


Aug 31st, 2005, 12:38 AM
Yikes. :)

The above is everything applicable.

Willy Duitt
Aug 31st, 2005, 12:53 AM
var str = new Array( "2003|name", "data" );

Aug 31st, 2005, 01:08 AM
Can you decipher that for me?

Sep 2nd, 2005, 09:59 PM
A lot of your questions may be answered by looking up / "Googling" "regular expressions"...

To answer your thread question, you can use:
myRegex.replace(myString, replacementString);

/* Translated regexDashes:
* / = opening regex
* (.*) = look for zero or more (eg *) characters (eg .) and make into group 1 (eg the ()'s )
* [\-\|\_] = look for -'s, |'s, and _'s; I had to escape (eg \) each of these so that they wouldn't apply any special meaning (eg - without escaping means a range)
* (.*) = make a second group of zero or more characters
* /g; = end the regular expression. Make this effective globally (g)
* For the second param in replace(), the $1 will insert that first capturing group (eg (.*) ), and the $2 will insert the second group.
var regexDashes = /(.*)[\-\|\_](.*)/g;
var replacedString = regexDashes.replace("my-original-string","$1 $2");

In answer to your latest reply:
the regex (eg the stuff inside the /'s, before .test...) means:
/^ = beginning of the line (not to be confused with [^a] which means not 'a')
(\d+) = digits (eg numbers 0-9 = \d) occuring 1 or more times (eg +) and being captured (eg ()'s ) as group $1
\| = the '|' character (again it was escaped with the backslash '\')
(\w+) = Group $2, this time looking for 1 or more word-characters (eg a-z, A-Z, 0-9, or _ = \w)
\, = a comma ',' character (escaped again)
(\w+) = $3 of 1+ word-characters
$/ = end of a line

Then the last three lines pop-up your 3 captured groups ($1-3 respectively)

Sep 4th, 2005, 04:58 PM