0

I'm trying to build a regex to determine if a string contains /i/ (no regex yet) This is complicated because the symbol / is already used in regex

I'm going at it like:

console.log('string/i/'.match(/i/))
coiso
  • 7,151
  • 12
  • 44
  • 63

1 Answers1

3

Since you aren't doing much "pattern matching" here, it's probably more intuitive to simply not use regular expressions in the first place, so you never have to worry about which characters do or don't need escaping:

console.log('string/i/'.indexOf('/i/') !== -1);

But if you must use regexp, simply escape the slashes with backslashes:

console.log(!!'string/i/'.match('\/i\/'));

Pretty much all of this is in the comments, but for some reason no one had made an answer out of it yet.

Ixrec
  • 966
  • 1
  • 7
  • 20