Forum Moderators: open

Message Too Old, No Replies

Need quick JS help

JavaScript parsing URL and replacing characters

         

lkalisky

6:57 am on Jun 5, 2008 (gmt 0)

10+ Year Member



Hi,

I'm trying to use JS to parse the URL of a page and replace characters within it. Specifically, I want to take a URL like this:

http://www.domain.com/classifieds/short_term_rentals-b408.html

and populate and print a variable with this:

short+term+rentals

Here is what I have:


var string=(location.href);
var getit= new Array();
var getit=string.split("classifieds/");
var result=getit[1];
var filename=result.replace("_","+");
var q=filename.split("-");
document.write(q[0]);

but this is only replacing the *first* underscore in the URL and is printing:

paris+short_term_rentals

How can I get it to loop and replace ALL the underscores?

Thanks for any help!

daveVk

7:38 am on Jun 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



TRY

var re = /_/g;
var filename=result.replace(re,"+");

in place of

var filename=result.replace("_","+");

lkalisky

8:06 am on Jun 5, 2008 (gmt 0)

10+ Year Member



excellent, thanks!