Home  >  Q&A  >  body text

REGEX works in chrome but breaks in safari

The following javascript regular expression causes SyntaxError in safari: Invalid regular expression: invalid group specifier name"

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

Can someone help me rewrite the regex that will work in safari?

I found out that safari doesn't support lookbehind, but still can't rewrite the whole regex, which would be useful for safari.

P粉506963842P粉506963842277 days ago541

reply all(1)I'll reply

  • P粉949190972

    P粉9491909722024-01-17 09:45:45

    Modify your schema to avoid negative reviews. Since you seem to want a non-whitespace character as the last character, just use a character class.

    /^(?!\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.

    reply
    0
  • Cancelreply