Home > front end >  Which regex modifiers are used with regex.test(string)?
Which regex modifiers are used with regex.test(string)?

Time:02-12

It seems there are 7 JS RegExp modifiers:

  • d indices
  • g global
  • i case
  • m multiline
  • s dotall
  • u unicode
  • y sticky

Which of the above are applicable for doing regex.test(string) ? For example, since it just tests whether the string contains one or more matches or not, isn't the g flag N/A for it? In other words:

/h/.test("h h h h") === /h/g.test("h h h h")

i, m, s, and u seem like they would be applicable, but I'm also not certain about d (I think not since it relates to indices and this just returns a bool?) or y (I think yes). Which modifiers make sense for the .test() method?

CodePudding user response:

If you look at the spec, you can see that it simply calls RegExp.exec and then coerces a null result to false, otherwise true.

All of the side-effects that apply to using exec apply to test.

  • Related