Regular expression solving: JavaScript regular expression matching problem

    A string can only appear once in a paragraph of
  1. text, for example: this is a-sharp-sharp regular expression. Among them, "- sharp-sharp" can only be matched once, and the order of occurrence is not fixed. How to solve this kind of regular expression?
  2. same as above, a string can only appear once in a paragraph of text, and can only appear at the beginning, for example:-sharp-sharp this is a regular expression. Where "- sharp-sharp" can only match once, "- sharp-sharp" can only appear at the beginning, and then no longer match "- sharp-sharp". How to solve this regular expression?

1, / (- sharp-sharp). *\ 1matches /
2, match 1 and then match whether to start with-sharp-sharp

A successful match indicates that there is a duplicate string


do you mean that a character can only appear once or a string can only appear once? This is different
1, can only occur once-sharp-sharp

^(?!.*(-sharp-sharp).*\1)(?=.*-sharp-sharp).+$

2.-sharp-sharp, can only occur once and can only start

^(?!.*(-sharp-sharp).*\1)-sharp-sharp.*$
Menu