1

All you need to know is in the question. I have a search query in the URL that looks like this:

http://www.mysite.com?param=word1+word2+word3

So far, I can retrieve "word1+word2+word3" but I can't seem to use something like:

document.write(str.replace(/+/g," "));

I want the final output to look like this: "word1 word2 word3"

Thanks for looking.

0

5 Answers 5

6

Try to escape the + sign using the escape char \ since is a restricted char:

document.write(str.replace(/\+/g," "));

http://www.regular-expressions.info/javascript.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I am glad the answer was helpful and fast :)
Thanks everyone, and mazzucci for being first :)
2

The + is a special character in regex, if you want to match it literally you have to escape it. So try:

document.write(str.replace(/\+/g," "));

Comments

2

You have to escape the + sign, it's has special meaning in Regexes.

document.write(str.replace(/\+/g," "));

Comments

0

How about using the split() function, that seems to be much simpler for this case:

document.write(str.split(",", " ");

Comments

0

Try with the hex value

document.write(str.replace(/[\u002B]/g," "));

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.