Regex string mismatch with pipe
I have the following regex
var catalogueHrefRegex = /\/automobiles\/(cr)|(ts$)|(zs)|(ax)|(wx)$/;
I am testing this against a url, for example
var href = "xxx/compare/ax?model=xxx";
I run the following code
catalogueHrefRegex.test(href)
I expected it to return false but it returns true. I am trying to URLs like
/automobiles/cr/...
/automobiles/ts/...
/automobiles/zs/...
/automobiles/ax/...
/automobiles/wx/...
I have the $ at the end of ts because I don't want to match
'automobiles/tsx' I tried the following but none of them worked as
expected
/\/automobiles\/((cr)|(ts$)|(zs)|(ax)|(wx))$/
/\/automobiles\/*(cr)|(ts$)|(zs)|(ax)|(wx)*$/
I am not good with regex, so my attempts were after some googling. I am
suspecting the way I set up the pipes were wrong or the grouping is
incorrect because when I try 'cr', it works as expected but I am not sure
how to group the cr/ts/zs/ax/wx so that it says match automobiles/ cr or
ts or ...
http://jsfiddle.net/WdXXx/
What is the correct regex for my desired results? Thank you for your time
No comments:
Post a Comment