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

javascript - How to make regex case-insensitive? - Stack Overflow

programmeradmin1浏览0评论

I have this javascript code but when i send this: asd.JPG the regex fails to me..

if (data.match(/([^\/\\]+)\.(jpg|jpeg|gif|png|tiff|tif)$/i))
     return { filename: RegExp.$1, ext: RegExp.$2 };
else
     return { filename: "invalid file type", ext: null };

So I want that the regex looks at the extension as case-insensitive. I tried this but it fails:

data.match(/([^\/\\]+)\.(?i)(jpg|jpeg|gif|png|tiff|tif)$/i)

Any Ideas?

I have this javascript code but when i send this: asd.JPG the regex fails to me..

if (data.match(/([^\/\\]+)\.(jpg|jpeg|gif|png|tiff|tif)$/i))
     return { filename: RegExp.$1, ext: RegExp.$2 };
else
     return { filename: "invalid file type", ext: null };

So I want that the regex looks at the extension as case-insensitive. I tried this but it fails:

data.match(/([^\/\\]+)\.(?i)(jpg|jpeg|gif|png|tiff|tif)$/i)

Any Ideas?

Share Improve this question edited Apr 21, 2011 at 13:19 T.J. Crowder 1.1m199 gold badges2k silver badges1.9k bronze badges asked Apr 21, 2011 at 13:12 NirNir 2,6299 gold badges47 silver badges75 bronze badges 10
  • 2 Your code works for me: jsfiddle.net/fkling/ecThT The i at the end of the regex makes it case-insensitive. – Felix Kling Commented Apr 21, 2011 at 13:15
  • 1 Welcome to StackOverflow! To the right when you were asking your question there was this handy How to Format box. Worth a read, as is the page linked from the [?] just above the question area. (I've fixed it for you on this occasion.) – T.J. Crowder Commented Apr 21, 2011 at 13:16
  • 1 Can you clarify what you mean by 'fail'? For what input are you getting an incorrect output? What output do you get and what do you expect? – Dancrumb Commented Apr 21, 2011 at 13:16
  • <s>**Okay, this is really weird** StackOverflow is showing that I added the backslash before the . in the question, but I did not. I just highlighted the code and clicked the {} button. @user: I've just gone and removed it again. If that backslash is in your code, you can go back and edit it back in.</s> The backslash was there, it's just that since it wasn't formatted as code, it wasn't showing. whew @user: This demonstrates the value of formatting the code, to avoid misunderstandings (@KooilInc's answer is based on that misunderstanding). – T.J. Crowder Commented Apr 21, 2011 at 13:19
  • @T.J.Crowder: Check the source of the original question, it is there. – Felix Kling Commented Apr 21, 2011 at 13:22
 |  Show 5 more comments

1 Answer 1

Reset to default 37

The i flag you have on the end (/.../i) should be doing it.

(CW because let's face it, one shouldn't earn rep for this sort of thing... :-) )

发布评论

评论列表(0)

  1. 暂无评论