2

I've been trying out different regexes but I can't seem to find the correct one.

I need an regex which allows users to type alphabetic, digits, spaces, - and ' in texts.

So strings like: "'s Ochtends vroeg" "tomorrow-night" "ISBN1234ABC"

should be true.

I've tried the following regexes with the following code:

/([A-Za-z0-9'-\s])/g
[^#$@_&!*()]
and more of these variations

   var regex = new RegExp("([a-zA-Z0-9\s'-])+");
   console.log(regex.test(word));

All return true now, when I type in the word: "ABN##@@123-TEST". It should be false because the characters ##@@ are in it.

Thanks for your help.

Elvira
  • 1,410
  • 5
  • 23
  • 50

1 Answers1

2

The new RegExp("([a-zA-Z0-9\s'-])+") regex finds partial matches, i.e. it finds streaks of letters, digits, ' or - inside a larger string, and will return the match once found. NOTE it does not find whitespace, because \s inside a string literal gets parsed as a letter s, not \s whitespace pattern.

So, you need to do 2 things:

  • Add anchors, ^ at the start and $ at the end of the pattern to ensure a whole string match
  • Use a regex literal notation, /regex/, to ensure \s is parsed as a whitespace pattern.

Note you do not need to wrap the whole pattern within a capturing group, you can always access the whole regex match value (even when replacing, with $& backreference). So, you may remove ( and ) around the pattern.

Thus, your solution is

var regex = /^[a-zA-Z0-9\s'-]+$/;

JS demo:

function test() {
  var str = document.getElementById("text").value;
  var regex = /^[a-zA-Z0-9\s'-]+$/;
  if (regex.test(str)) {
     console.log("Valid");
  }
  else {
     console.log("Not Valid");
  }
}
<input id="text" type="text" />
<button id="btn" onclick="test()">Test</button>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I need to ask you one more thing.. how can I accept characters with ë ö ï ï ê and that kind of stuff? For names/words like Chloë. – Elvira Oct 17 '17 at 09:24
  • Use [`XRegExp`](http://xregexp.com/) and you will be able to use `\pL` that matches any Unicode letter. Else, build a Unicode code point range for the accepted letters, see [Javascript + Unicode regexes](https://stackoverflow.com/questions/280712/javascript-unicode-regexes). Current native regex JS implementation does not support Unicode property classes, unfortunately. Also, see [this answer of mine](https://stackoverflow.com/questions/34849097/preg-match-to-regex-equivalent-expression/34854255#34854255), or better import XRegExp [like here](https://stackoverflow.com/a/45849033/3832970). – Wiktor Stribiżew Oct 17 '17 at 09:25
  • If you are after matching some European letters only, see [Regular expression not working for at least one European character](https://stackoverflow.com/questions/30798522/regular-expression-not-working-for-at-least-one-european-character/30798598#30798598), adding `À-ÿ` might be all you want in your pattern. Since that range includes mutliplication and division signs, you may add `À-ÖØ-öø-ÿ` instead of `À-ÿ`. – Wiktor Stribiżew Oct 17 '17 at 09:30
  • @Elvira This requirement is out of scope in the current question. There are lots of other threads here discussing how to match Unicode chars with regex in JS. – Wiktor Stribiżew Oct 18 '17 at 06:32