Zauważyłem, że apple-juice
powinien być zgodny z Twoimi parametrami, ale co z apple juice
? Zakładam, że jeśli sprawdzasz poprawność apple juice
nadal chcesz, aby się nie udało.
Tak - pozwala zbudować zestaw znaków liczyć jako „granicy”:
/[^-a-z0-9A-Z_]/ // Will match any character that is <NOT> - _ or
// between a-z 0-9 A-Z
/(?:^|[^-a-z0-9A-Z_])/ // Matches the beginning of the string, or one of those
// non-word characters.
/(?:[^-a-z0-9A-Z_]|$)/ // Matches a non-word or the end of string
/(?:^|[^-a-z0-9A-Z_])(apple|orange|juice)(?:[^-a-z0-9A-Z_]|$)/
// This should >match< apple/orange/juice ONLY when not preceded/followed by another
// 'non-word' character just negate the result of the test to obtain your desired
// result.
W większości smaków regexp \b
liczy się jako „granicy słowa” ale standard listy „znaków słownych” robi” t obejmują -
, więc musisz utworzyć niestandardową. To może się równać z /\b(apple|orange|juice)\b/
jeśli nie próbowali złapać -
także ...
Jeśli tylko testowanie testy „pojedyncze słowo” można przejść z dużo prostsze:
/^(apple|orange|juice)$/ // and take the negation of this...
Z jakim językiem współpracujesz? Również "sok pomarańczowy" powinien się zgadzać, czy nie? – gnarf