0

I have this string:

var s = '/channels/mtb/videos?page=2&per_page=100&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true'

I want to repace per_page number (in this case 100, but it can be any number from 1-100, maybe more?)

I can select first part of the string with:

var s1 = s.substr(0, s.lastIndexOf('per_page=')+9)

which give me:

/channels/mtb/videos?page=2&per_page=

but how would I select next '&' after that so I can replace number occurrence?

dont assume same order of parameters!

2
  • I'd recommend doing this kind of replacement with a regex rather than lastIndexOf and substr. Commented Mar 14, 2018 at 18:22
  • the first parameter to the substr is the start index of where to begin the search so you can start from the end of your first search and then look for the next & Commented Mar 14, 2018 at 18:24

5 Answers 5

3

You can use following regex to replace the content you want.

regex:- /per_page=[\d]*/g(this is only for your requirement)

var new_no=12;  //change 100 to 12
var x='/channels/mtb/videos?page=2&per_page=100&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true';

var y=x.replace(/per_page=[\d]*/g,'per_page='+new_no);
console.log(y);

Explanation:-

/per_page=[\d]*/g

/          ----> is for regex pattern(it inform that from next character onward whatever it encounter will be regex pattern)
per_page=  ----> try to find 'per_page=' in string 
[\d]*      ----> match 0 or more digit (it match until non digit encounter)
/g         ---->/ to indicate end of regex pattern and 'g' is for global means find in all string(not only first occurrence) 
Sign up to request clarification or add additional context in comments.

Comments

2

Use replace with a regular expression to find the numbers after the text per_page=. Like this:

s.replace(/per_page=\d+/,"per_page=" + 33)

Replace the 33 with the number you want.

Result:

"/channels/mtb/videos?page=2&per_page=33&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true"

1 Comment

[?&]per_page= would be better at it matches the key exactly
1

Start with the index from the lastIndexOf-per_page instead of 0. Get the index of the first & and create a substr s2 to the end. Then concat s1 + nr + s2. I would not use regex, because it is much slower for this simple stuff.

Comments

1

With Array.filter you can do this, where one split the text into key/value pairs, and filter out the one that starts with per_page=.

Stack snippet

var s = '/channels/mtb/videos?page=2&per_page=100&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true'

var kv_pairs = s.split('&');
var s2 = s.replace((kv_pairs.filter(w => w.startsWith('per_page=')))[0],'per_page=' + 123);

//console.log(s2);

Comments

0
var matches = /(.*\bper_page=)(\d+)(.*)/;

if (matches) {
  s = matches[0] + newValue + matches[2];
}

2 Comments

I'm not even sure that this submission answers the original question. Even if it did, I would suggest providing some commentary.
Although this does seem to answer the question, providing some explanation will improve the value of the answer. Please try to avoid code-only answers. Best regards

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.