4

How do you match more than one pattern in vbscript?

Set regEx = New RegExp

regEx.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.

2 Answers 2

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

param = regEx.Replace(param, "$1") ' The $1 brings our ? or & back
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but how would I string two completely different patterns together? Let's say remove anything that matches "dog" or "cat"?
Thanks, although in this case, the $1 will return the ? or & of every match, so I get string&&
2

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-]+

1 Comment

Thanks. I was doing the | before, but I realized the reason it wasn't working properly was because I didn't have regEx.Global = True set.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.