0

I am trying to search if dash exist at start or the end of the string

It working fine for Chrome but unfortunately Firefox not yet support.

Here my regex code

/^(?!-)(?!.*--)[A-Za-z0-9-]+(?<!-)$/;

I expect on Firefox also catch when the word:

-test

test-

-test-

  • 1
    Possible duplicate of [Javascript regex negative lookbehind not working in firefox](https://stackoverflow.com/questions/50011366/javascript-regex-negative-lookbehind-not-working-in-firefox) – Thum Choon Tat Aug 23 '19 at 02:46
  • Yes similar problem but I am not so sure. how to understand that syntax and apply to my case – Mark Chhunlong Aug 23 '19 at 02:52

1 Answers1

0

Separate out the character set into two parts: one for (all but the last character) and one for (the last character), and then simply remove the - from the last character set:

/^(?!-)(?!.*--)[a-z\d-]*[a-z\d]$/i

([a-z\d] with the case-insensitive flag is equivalent to [A-Za-z0-9] without the flag)

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320