0

I am using replace method of string,

     var text = this ex is not working.;
     text = text.replace(" ", "+");
     alert(text);

and got alert :

     this+ex is not working.

Whats the problem here?

2 Answers 2

2

Try this: lil diff demo http://jsfiddle.net/bLaZu/6/

Please note:

The g flag on the RegExp, it will make the replacement globally within the string.

If you keen: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

Rest feel free to play around with demo, :)

code

var text = "this ex is not working.";
     text = text.replace(/\s+/g, '+');
     alert(text);​
Sign up to request clarification or add additional context in comments.

2 Comments

This answer is little more informative for novice than previous one. Voting for this answer.
@NikolayZakharov Glad you liked it, Have fun, see you around :)
1

To replace all spaces with plus + character use the following:

var text = "this ex is not working.";
text = text.replace(/ /g, "+");
alert(text);

And don't forget to use quotes " for initializing strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.