0

i'm trying to write regular expressions to match these exact strings:

  • "i'm in"
  • "i'm out"

i'm not searching for a single character. i'm searching for an exact string and trying to understand how reg exs work.

i've read the MDN docs and the chapter in Eloquent JS, as well as searched on stack overflow conversations.

i understand that anything in square brackets represents a character set and the characters do not have to occur in a sequence.

i tried /[i\'m] in/ which works in my code but when i run:

/[i\'m] in/.exec('i\'m in') it evaluates to ["m in"]

i also tried /i\'m in/ which doesn't work in my code and when but when i run:

/i\'m in/.exec('i\'m in') it evaluates to ["i'm in"]

not sure why this is so. any pointers or illuminations would be great.

Community
  • 1
  • 1
tea
  • 568
  • 2
  • 10
  • 18
  • The apostrophe character is not special in regular expressions. You don't have to quote or escape it. `/i'm (in|out)/` should do it. If you want to match those *complete* exact strings, `/^i'm (in|out)$/` – Pointy Dec 03 '16 at 06:14
  • Also the `.exec()` function returns an array of matched pattern parts. Your last example is working properly. What did you expect it to return? – Pointy Dec 03 '16 at 06:17
  • If you want to see what a regular expression matches, you could play with http://www.regexpal.com/ – some Dec 03 '16 at 06:29
  • If you want to match those strings you could use the expression `/^i'm (in|out)$/` where the ^ anchors the string at the start of the string and $ at the end of the string. – some Dec 03 '16 at 06:34
  • The [] is used to specify what characters should match (or not match) and can be a range, for example `[a-f]` matches ONE character in the string, if it is a lowercase a, b, c, d, e or f. `[a-f]{2}` can be used to match TWO characters, like `aa`, `bc` or `fa`. `[0-9a-fAF]{2}` matches any two-digit hexadecimal value, regardless of case, example: `0a`, `F3` or `Bc`. – some Dec 03 '16 at 06:51

1 Answers1

1

Your regex matches only 4 characters while your complete string is 6 characters long.

/[i\'m] in/.exec('i\'m in')

Regex --> [i\'m] in

It will match 4 characters. The first character can be either i or \' or m. The second character is a space and third character is i and fourth character is n. You don't have to use \ for ' in regex. But you will need to use \ for ' in JS string.

String --> 'i\'m in'
m in is the string the regex matches. It can also match these strings: i in, ' in

If you want to match all 6 characters of string you can use something like this:

/i'm in/.exec('i\'m in')

Demo: https://repl.it/EfDS/3

And if you want to match either in or out, you can use an or operator.

/i'm (in|out)/.exec('i\'m out')

Demo: https://repl.it/EfDS/4

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
  • Thanks so much. This helps a lot. – tea Dec 03 '16 at 06:30
  • 2
    Why are you using `[i\'m]{3}` ?? That's not what the person who wrote the question wanted. It matches `iii`, `ii'`, `iim`, `i'i`, `i''`, `i'm`, `imi`, `im'`, `imm`, `'ii`, `'i'`, `'im`, `''i`, `'''`, `''m`, `'mi`, `'m'`, `'mm`, `mii`, `mi'`, `mim`, `m'i`, `m''`, `m'm`, `mmi`, `mm'` and `mmm`. What the person wanted was something like `/^i'm (in|out)$/` – some Dec 03 '16 at 06:44
  • Yes you are right. I'll correct it. But the point of emphasis was that his regex was matching only 4 characters. Thanks. – Mohammad Yusuf Dec 03 '16 at 06:49
  • I disagree. The poster was trying to learn and did a mistake with the syntax. You answer has now made the poster believe that's the way it should be done. – some Dec 03 '16 at 06:59
  • Ok I agree with you. But he learned about length though :). Now he will crash his regex somewhere and comeback again to learn why not to use []{3}. And I learned from you to address all the points :). – Mohammad Yusuf Dec 03 '16 at 07:11