简体   繁体   中英

Regex patern to match dimensions from a string

I apologize if this is a duplicate, but I've searched for 3 hours already and couldn't find anything that doesn't deal with only a single pattern.

I use a dimensions parameter in a URL (ie ...media.php?sid=200x100&mid=... where 200 is width and 100 is height). There are other parameters that may/may not include other numbers (ie UUID's) but are not important to this task but should not affect the pattern match.

What I need to do is match the 'sid=###x###' portion only in order to replace it. The ### should support variable length numbers in a range from 1 to say 99999.

I've tried a lot of patterns so far and suspect someone will probably know this off the top of their head so I'm not going to list ALL of my attempts, but here's the basic code I'm using:

var oldSid = new RegExp('sid=[0..99999]x[0..99999]');
var newSid = 'sid=500x500';
var newUrl = oldUrl.replace(oldSid, newSid);

Any help you can offer to fill in the blanks would be greatly appreciated!

Use regex sid=[0-9]{1,5}x[0-9]{1,5} to find such match...

Test it here .

Use this one:

var oldSid = new RegExp("sid=\\d{1,5}x\\d{1,5}");

...or (as it's not a dynamic regular expression) this one:

var newUrl = oldUrl.replace(/sid=\d{1,5}x\d{1,5}/, newSid);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM