1

I have a regex:

values.communicators.forEach(e => {
    let isValid = /(?=.*[a-zA-Z\d].*)[a-zA-Z\d!@#$&-. )(:]{2,}/gi.test(e.name);
    console.log(isValid)
    if (!isValid) {
        errors.communicators = 'Unsupported character or length in communicator names.'
    }
})

When I use the like above in code something like Some Diddy∞∞ will pass but at https://regexr.com/ it will not.

Does anyone know what may be going on?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Quesofat
  • 1,493
  • 2
  • 21
  • 49
  • 2
    It will on regexr.com too. If you need to test on entire input string you should add `^` and `$` anchors: `^REGEX$` – revo Apr 02 '18 at 21:19
  • So I'm curious: does PHP require this too? Also what does it check against without `^` and `$`? @revo – Quesofat Apr 02 '18 at 21:24
  • PHP needs it to match against whole input string as well. When those anchors are not available a partial match may happen that satisfies engine but doesn't satisfy your requirements. – revo Apr 02 '18 at 21:27
  • 1
    You might be getting hit with the `lastIndex` attr that plagues many unsuspecting devs when working with the `g` flag. – Pytth Apr 02 '18 at 21:37

0 Answers0