PDA

View Full Version : Replacing "this" with "this"...



pcbrainbuster
04-12-2007, 06:05 PM
Hello again guys :),

Sometimes when a user types down something you may need to replace certain characters/words with others, eg -

<html>
<body>
<input type="text" id="tbox">
</body>
</html>

Say tbox's value was c:\document and settings\john\desktop\, what script/method would you use to replace every instance of \ with?

Thanks :)...

Bob90
04-12-2007, 06:14 PM
to replace all backslashed with forward slashes


string.replace(/\\/g,"/")

/ / = regular expression
\\ is an escaped backslash
g means gloablly (All occurances)


:)

mburt
04-12-2007, 06:30 PM
Say tbox's value was c:\document and settings\john\desktop\, what script/method would you use to replace every instance of \ with?

Hmm.. it seems pcbrainbuster hasn't specified what he/she wants to replace \ with. The above regex does work, we just need to know what to replace \ with.

pcbrainbuster
04-12-2007, 06:33 PM
Replace it with !... And can't it be done without regex???!!!

mburt
04-12-2007, 06:35 PM
Er... replace it with what, an exclamation point?

And can't it be done without regex???!!!
Believe me, doing it without regular expressions would be much more complicated.

pcbrainbuster
04-12-2007, 06:39 PM
Yup, replace it with ! (remember that is to be used as an example and its not practical...), I don't understand regex (yet) so can I see the normal way of doing it (without regex)...

Thanks :)...

mburt
04-12-2007, 07:23 PM
Sorry, but there is no "normal" way of doing it. The replace method's arguments are regular expression strings only.

pcbrainbuster
04-12-2007, 07:48 PM
Neverming I searched how to do it and now can do it...

boogyman
04-12-2007, 07:49 PM
<script type="text/javascript">
// <![CDATA[

function replaceIt() {
str = "I want to replace &#92 with a !";
newStr = str.replace("&#92", "!");

document.write("<b>String:</b> " +str + "<br /><b>Result:</b> " + newStr);
}

// ]]>
</script>


Result is


String: I want to replace \ with !
Result: I want to replace ! with !


sorry I got my code mixed up... using the replace method is alot faster then using regular expressions for simple tasks.

Bob90
04-12-2007, 07:54 PM
I think you can replace words (letters) with easy regex like /word/, but other characters like backslash require more complicted regex.

mburt
04-12-2007, 08:26 PM
Plus the g identifier for making it global. That shouldn't work, seeings the first argument in the string method has to be a regular expression.
It appears though, that the problem has been solved.

Twey
04-12-2007, 09:45 PM
the first argument in the string method has to be a regular expression.Nope, it can be a string or a regex, and the second parameter can be a string or a function.

The problem with this code is that it will replace a backslash with an exclamation mark... exactly once.
"/ / aoeui /".replace("/", "!"); // returns "! / aoeui /"
"\\ aoeui \\ \\".replace("\\", "!"); // returns "! aoeui \\ \\"If you want to replace all instances, you have to use regex with the /g flag:
"\\ aoeui \\ \\".replace(/\\/g, "!"); // returns "! aoeui ! !"Mozilla's JavaScript implementation, SpiderMonkey, does include an optional third parameter, flags to use with the string, which works like so:
"\\ aoeui \\ \\".replace("\\", "!", "g"); // returns "! aoeui ! !"... but it only exists in Mozilla-based browsers, so it's not really a viable option.

mburt
04-13-2007, 12:32 AM
Which is why a simple regular expression would be a better idea.

jscheuer1
04-13-2007, 05:13 AM
Regular expressions and the replace method are the normal way to do replacements in strings.

Splitting the string into an array based upon the character you wish replaced and then joining it back into a string based on the replacement character can sometimes be more efficient:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function changeSlash(s, c){
return s.split('\\').join(c);
}
</script>
</head>
<body>
<input id="test" size="50" type="text" value="c:\bobs\your\uncle\index.htm">
<input type="button" onclick="document.getElementById('test').value=changeSlash(document.getElementById('test').value,'!');">
</body>
</html>

It's not in this case though.