0

I have a string url with following structure:

url/inbox/g/{page_number}

I am in a situation where I need to replace {page_number} with a new value. So I need somehow to search for url/inbox/g and replace it with url/inbox/g/{new_value}.

How to use replace() function to achieve this?

5
  • is {page_number} the literal string "{page_number}" or is it an actual number? Also, when you say you need to search for url/inbox/g where are you searching for that? Is there a larger text where you need find all mentions of a URL and replace them, or is it just the URL itself you get as input? Commented Sep 14, 2016 at 22:09
  • @vlaz it is an actual number. by search, i mean the first parameter of replace function. thank you Commented Sep 14, 2016 at 22:10
  • Have you considered a RegEx? Commented Sep 14, 2016 at 22:10
  • @PM77-1, yes this seems to be the only solution since page_number is dynamic. I just don't know how to Commented Sep 14, 2016 at 22:13
  • 3
    You can also use .split() to split up the URL at / characters, replace the last element of the array, then join them back with .join(). These are all simple operations. Commented Sep 14, 2016 at 22:14

5 Answers 5

1

Due to the number is the last portion of the string, you may use non regex solution using lastIndexOf and slice:

<script>
  url = 'url/inbox/g/44';
  replaceNumStr = 'Dummy';
  newVal = url.slice(0,url.lastIndexOf('/')+1);
  alert(newVal+replaceNumStr);
 </script>

Checkout this demo

Sign up to request clarification or add additional context in comments.

Comments

1

Use a replace statement like this:

var newVal  = 'anything',
    pattern = "/url\/inbox\/g\/\d+$/",
    reg     = new RegExp(pattern, "i");
    repURL  = url.replace(reg, "url/inbox/g/" + newVal);

I am assuming url is dynamically accessed.

1 Comment

Thank you, I will try to use your RegExp and feedback, thanks
1
var url = "url/inbox/g/4321"
var your_number = 1234
url = url.replace(/\d+$/, your_number)

3 Comments

that's not going to work because {page_number} is going to be a number which changes each time so it can't be hardcoded into the replace function call
the value of page_number is rendered served side, I don't have control of it. It can be 1 digit or more
right, totally misread the question. my bad. that regex should be what you're looking for in my updated answer
1

I would use the following regular expression : /\/\d*$/m And the replacement could be done with :

str.replace(/\/\d*$/m, "/" + n)

Where n is the new value

The regular expression says find everything that match "/" followed by 0 or more digits and which end the string. Reference

1 Comment

Thank you, I will try to use your RegExp and feedback, thanks
1

Two other possibilities, not using RegExp.

const url = 'url/inbox/g/{page_number}';
const parts = url.split('/', 3);
const new_number = '{new_page_number}';
parts.push(new_number);
const new_url1 = parts.join('/');
console.log(new_url1);

const url = 'url/inbox/g/{page_number}';
const new_number = '{new_page_number}';
const new_url2 = `${url.slice(0, url.lastIndexOf('/'))}/${new_number}`;
console.log(new_url2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.