string replace

by GJSenthil 2007-11-29 12:35:05

Hi,
In Javascript, String replace may be used in many cases where the string can be replaced with space, comma or else with some characters etc.

For example if we want to replace two characters we can use replace method by

str = new string(vikasraj)
str = str.replace(k,"l");

The above will replace vikas to vilas where a single replace takes place. Whereas to makes the replace globally we can use /g along with the above replace format

str = str.replace(/a/g,"e")

This will replace vikasraj to vikesrej. If we want to replace the special charaters it will not works with the above replacement format. There we can use the regular expressions where it can be used globally also.

str = new string("http://hiox.org+bypass+addscraps.php")
str = str.replace(/\+/g,"/")

The result would be http://hiox.org/bypass/addscraps.php.Here regular expression plays its role in replacing the slash with plus sign.






Tagged in:

1234
like
0
dislike
0
mail
flag

You must LOGIN to add comments