Toniq
Toniq

Reputation: 4996

Javascript string replace certain characters

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!

Upvotes: 0

Views: 90

Answers (5)

Asons
Asons

Reputation: 87191

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);

Upvotes: 1

yajiv
yajiv

Reputation: 2941

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) 

Upvotes: 3

Markus
Markus

Reputation: 597

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.

Upvotes: 1

R. Schifini
R. Schifini

Reputation: 9313

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"

Upvotes: 2

kshetline
kshetline

Reputation: 13672

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

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

Upvotes: 0

Related Questions