tags:

views:

241

answers:

1

i having a string variable

var text = "hello hw r u";

And i need to replace 'h' to '*' and 'l' to '-'

hw to do that..

+4  A: 
text = text.replace(/h/g, "*").replace(/l/g, "-");

In answer to you comment below

* is a special character in a Reqular Expression pattern, you need to escape it using a backslash (\) character. So it would be

replace(/\*/g, 'o')

See this quick guide on JavaScript Regular Expressions

Russ Cam
we can't do it in a single replace call...?
santose
then split it into two.
rahul
You want to match two different strings and replace them with two different strings. I don't see how you could do that in one replace call
Russ Cam
how to replace '*' char. its taking as comment whn i usereplace(/*/g,'o');
santose