Home > Software engineering >  REGEX working in chrome but breaks in safari
REGEX working in chrome but breaks in safari

Time:01-17

The following javascript regex breaks in safari getting SyntaxError: Invalid regular expression: invalid group specifier name"

/^(?!\s)[A-Za-z0-9\'\.\-\,\s]*(?<!\s)$/.test('ABCD@#');

Can someone please help me to re write the regex which can work in safari?

I find out that safari doesn't support lookbehind but still not able to re write the whole regex which can be good for safari.

CodePudding user response:

Modify your pattern to avoid the negative lookbehind. Since you seem to want a non whitespace character as the last character, just use a character class for that.

/^(?!\s)[A-Za-z0-9'.,\s-]*[A-Za-z0-9'.,-]$/.test('ABCD@#')

Side note: your current pattern looks wrong for what you are trying to match.

  • Related