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?

2
  • possible duplicate of Get value of a string after a slash in JavaScript Commented Mar 18, 2015 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. Commented Mar 18, 2015 at 16:30

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.