if(/categories/.test(url)) { ... }
I'm using the above code to check whether url
contains the string "categories". What is the syntax for checking to see if url
container the string "categories" OR "character"?
Thanks!
if(/categories/.test(url)) { ... }
I'm using the above code to check whether url
contains the string "categories". What is the syntax for checking to see if url
container the string "categories" OR "character"?
Thanks!
Share Improve this question edited Jan 19, 2012 at 7:17 Shiplu Mokaddim 57.7k20 gold badges145 silver badges192 bronze badges asked Jan 19, 2012 at 7:08 HandiworkNYC.HandiworkNYC. 11.1k25 gold badges95 silver badges156 bronze badges5 Answers
Reset to default 4The vertical pipe |
denotes "or" in regular expressions
if(/categories|character/.test(url)) { ... }
You can do something like this:
if(/(categories)|(character)/.test(url)) { ... }
As shown on here, the 'pipe' (|
), denotes OR in a regular expression.
if(/(categories|character)/.test(url)) { ... }
...should work.
I find this resource very helpful when dealing with regular expressions: http://www.regular-expressions.info/reference.html
You dont need regex for that
String functions are enough for that.
if(url.indexOf("categories")>0 || url.indexOf("character")>0){
// your code.
}
if(/(categories | character)/.test(url)) { ... }