简体   繁体   English

正则表达式模式以匹配字符串的尺寸

[英]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. 如果这是重复的操作,我深表歉意,但是我已经搜索了3个小时,找不到任何仅能处理单个模式的内容。

I use a dimensions parameter in a URL (ie ...media.php?sid=200x100&mid=... where 200 is width and 100 is height). 我在URL中使用了一个Dimensions参数(即... media.php?sid = 200x100&mid = ...其中200是宽度,100是高度)。 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. 还有其他参数可能/可能不包括其他数字(即UUID),但对此任务并不重要,但不应影响模式匹配。

What I need to do is match the 'sid=###x###' portion only in order to replace it. 我需要做的只是匹配“ sid = ### x ###”部分以便替换它。 The ### should support variable length numbers in a range from 1 to say 99999. ###应该支持1到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... 使用regex sid=[0-9]{1,5}x[0-9]{1,5}找到这样的匹配项...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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