46

I've been trying to create a regex which would ignore the casing.

This is the regex i am trying to use:

/^[A-Za-z0-9._+\-\']+@+test.com$/;

So basically i would want to match any of these

  • abc@Test.com
  • abc@TEST.com
  • abc@teSt.com

I tried this, but it doesn't work:

/^[A-Za-z0-9._+\-\']+@+(?i)+test.com$/;

I read somewhere about the use of (?i), but couldn't find any examples which show their usage in regex to ignore casing. Thoughts anyone ? Thanks a lot in advance.

Community
  • 1
  • 1
Nanu
  • 3,010
  • 10
  • 38
  • 52

2 Answers2

98

Flags go at the end.

/regex/i

i is for case-Insensitive (or ignore-case)

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
  • Thanks, that worked. i wonder why there are not much references to the usage of this. Such a small but powerful thing. – Nanu Jun 24 '14 at 20:30
  • There are references everywhere; it's even [indirectly answered by this question](http://stackoverflow.com/questions/1186058/) – Evan Davis Jun 24 '14 at 20:53
  • Maybe you didn't know it's called _flags_? A google search for "js flags" brings up 122,000 results. – Evan Davis Jun 24 '14 at 20:54
  • I was searching "Regex ignore case" and that didn't help. And i believe that this sentence is also pretty simple straight forward obvious sentence, was hoping to get something out of it. Even at stackoverflow, it didn't return useful results. And yeah i honestly didn't know that these are called flags. Thanks for your help. – Nanu Jun 24 '14 at 21:11
  • 1
    The question I linked is the second result for that query. In any case, glad you got what you needed. – Evan Davis Jun 24 '14 at 21:33
  • I am not sure how the question that you have linked would help, may be i am new and still learning, but i don't see any reference to the usage of flag (i) there, and also it doesn't talk about its usage, that it needs to be at the end. Buthow the answer you have mentioned here is clean. – Nanu Jun 24 '14 at 21:49
  • how to ignore leading and trailer spaces and case – HaBo May 31 '16 at 16:43
27

For anyone else who arrives here looking for this, if you've got code that is using the RegExp constructor you can also do this by specifying flags as a second argument:

new RegExp(pattern[, flags])

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

For example:

var pattern = /^[A-Za-z0-9._+\-\']+@+test.com$/;
var regExp = new RegExp(pattern, "i");
garryp
  • 5,508
  • 1
  • 29
  • 41