Rick
Rick

Reputation: 273

Jquery url replace

How can I modify this:

/services/type/single_dwelling/

to this:

/ajax/services/single_dwelling/development

Currently I have:

linkUrl = $(this).attr("href").replace(/(\/(services)\/)/,"$1ajax/");

Which outputs:

/services/ajax/type/single_dwelling/

I'm a little confused.

Upvotes: 1

Views: 1291

Answers (2)

Ateş Göral
Ateş Göral

Reputation: 140152

Try:

linkUrl = "/ajax" + $(this).attr("href").replace("/type", "") + "development";

Upvotes: 1

Christian C. Salvadó
Christian C. Salvadó

Reputation: 828002

Looking carefully I see you want to prepend '/ajax', remove '/type' from the original url, and append 'development':

var linkUrl = '/ajax' + $(this).attr("href").replace('type/','') + 'development';

That will output "/ajax/services/single_dwelling/development" using your test string.

Upvotes: 6

Related Questions