0

I have a URL like this:

http://localhost/Customer/Edit/3

I need to check Customer/Edit/3 and replace the 3 with current id (9) in jQuery.

The final URL should look like http://localhost/Customer/Edit/9

Note: the replace must only work for Customer related URL.

How can I do this?

MMM
  • 7,221
  • 2
  • 24
  • 42
Nabin
  • 81
  • 1
  • 1
  • 11
  • possible duplicate of [Get value of a string after a slash in JavaScript](http://stackoverflow.com/questions/8376525/get-value-of-a-string-after-a-slash-in-javascript) – Daniël Camps Mar 18 '15 at 16:30
  • You're going to need to craft a regular expression to match Customer/Edit/3 and do myString.replace(). Try writing some code and come back here once you have a specific bug. – Hayden Schiff Mar 18 '15 at 16:30

1 Answers1

1

You don't need jQuery for this:

var pattern = /(http:\/\/localhost\/Customer\/Edit\/)\d/;
var str = "http://localhost/Customer/Edit/3";
str = str.replace(pattern, '$1' + 9);
console.log(str); // returns http://localhost/Customer/Edit/9

The above should be enough for you to create a solution that works for you.

If you have a guarantee that there is only one number in the URL, you can do the following instead:

var pattern = /\d/;
var str = "http://localhost/Customer/Edit/3";
str = str.replace(pattern, 9);
console.log(str);
MMM
  • 7,221
  • 2
  • 24
  • 42