本文介绍了正则表达式模式以匹配字符串中的维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是重复的,我很抱歉,但我已经搜索了 3 个小时,但找不到任何不只处理单一模式的内容.

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.

我在 URL 中使用尺寸参数(即 ...media.php?sid=200x100&mid=... 其中 200 是宽度,100 是高度).还有其他参数可能/可能不包括其他数字(即 UUID),但对这项任务并不重要,但不应影响模式匹配.

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

我需要做的是匹配 'sid=###x###' 部分只是为了替换它.### 应该支持范围从 1 到 99999 的可变长度数字.

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!

推荐答案

使用这个:

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

...或者(因为它不是动态正则表达式)这个:

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

这篇关于正则表达式模式以匹配字符串中的维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:32
查看更多