开发者

vbscript multiple replace regex

How do you match more than one pattern in vbscript?

Set regEx = New RegExp

reg开发者_Python百科Ex.Pattern = "[?&]cat=[\w-]+" & "[?&]subcat=[\w-]+" // tried this
regEx.Pattern = "([?&]cat=[\w-]+)([?&]subcat=[\w-]+)" // and this

param = regEx.Replace(param, "")

I want to replace any parameter called cat or subcat in a string called param with nothing.

For instance

string?cat=meow&subcat=purr or string?cat=meow&dog=bark&subcat=purr

I would want to remove cat=meow and subcat=purr from each string.


regEx.Pattern = "([?&])(cat|dog)=[\w-]+"

param = regEx.Replace(param, "$1") ' The $1 brings our ? or & back


Generally, OR in regex is a pipe:

[?&]cat=[\w-]+|[?&]subcat=[\w-]+

In this case, this will also work: making sub optional:

[?&](sub)?cat=[\w-]+

Another option is to use or on the not-shared parts:

[?&](cat|dog|bird)=[\w-]+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜