tags:

views:

25

answers:

4

Hi,

I insert some strings in an array but before I do that I want to do what topic says. To only replace space with %20 I do:

Name.push(linkText.replace(" ", "%20"));

But how do I perform two "replace" there in one go?

Thanks in advance

A: 

Do you want to replace two spaces in a row with one %20?

Name.push(linkText.replace(/ +/g, "%20"));

Or do you want to replace 2 spaces with %20%20?

Name.push(linkText.replace(/ /g, "%20"));
jtbandes
I'm sorry if I was unclear with my question. I want to remove any / and also replace a space with %20.so for example one two/three will be one%20twothree
Peter
A: 

You could do something like this:

Name.push(linkText.replace(" ", "%20").replace("/", ""));

Andrew Cooper
A: 

You can't do it using a single function call in regular JavaScript.

You could do this:

Name.push(linkText.replace(" ", "%20").replace("/", ""));
Dumb Guy
+2  A: 

It looks to me like you are trying to encode plaintext to use it in a URL or query string. I suspect you would be better off using one of javascript's built-in encoding methods, encodeURI or encodeURIComponent. See:

http://www.javascripter.net/faq/escape.htm

RMorrisey