最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Regex Test Function: *OR* syntax - Stack Overflow

programmeradmin2浏览0评论
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 badges
Add a ment  | 

5 Answers 5

Reset to default 4

The 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)) { ... }

发布评论

评论列表(0)

  1. 暂无评论