1

I need to validate input: when user type one of 3 words from list (array is in js file), I need to have error (in this case below, if user type test1, test2, or test3 you will see error, otherwise everything should be fine)

file.html

<input type="text" ng-pattern="regexPatter" />

file.js

var array = ["test1", "test2", "test3"]

var excludeWords = array.join('|')

$scope.pattern = new RegExp("\b(?!\b" + excludeWords + "\b)[a-zA-Z']+\b");

I found that this regex pattern should work, but unfortunately, it's problem with '\b' because js misinterprets this char.

How can I replace it? Maybe is another solution for this case? I can't just put this regex pattern in html, because I should add variable.

Thanks for any tip!

emka26
  • 433
  • 1
  • 11
  • 28
  • No, it doesn't work, now everything is valid. – emka26 May 29 '18 at 19:22
  • 1
    Give a shot with `new RegExp("^(?!.*("+excludeWords+")).*$"` – Thomas Ayoub May 29 '18 at 19:42
  • @ThomasAyoub great, it works like a charm! It's exactly what I need, but to be honest. I don't understand this code. Can you explain what means those chars? Especially this piece of code: .*("+excludeWords+")). Thanks! – emka26 May 29 '18 at 20:03
  • Then why did you use a regex with word boundaries? It is a classical regex to match a string not containing a word. – Wiktor Stribiżew May 29 '18 at 20:10

0 Answers0