4

I have this url http://retail.domain.co.uk/view_orders/page/6/10?type=completed

I want to replace 6 with 1. The url segment will be different depending on page, so i can't use a string replace.

var url = location.href.replace(, "1");

the new url should be http://retail.domain.co.uk/view_orders/page/1/10?type=completed

How do i solve this with regex?

1
  • there is some example in this website : regex101.com Commented Oct 6, 2015 at 11:01

2 Answers 2

5

Use a captured group to match /page/ that is followed by 1 or more digits:

var str = 'http://retail.domain.co.uk/view_orders/page/6/10?type=completed';

var result = str.replace(/(\/page\/)\d+/, '$11');

RegEx Demo

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

Comments

1

You may do like this,

string.replace(/\/\d+\//g, "/1/")

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.