title: Regex Pattern for Textfield
body: <p>I am trying to write regex pattern for my textfield. I want it to accept letters, numbers as well as two special characters(underscore and hyphen). It can contain any number of words, and no other special characters are allowed except underscore and hyphen in between the words or starting/end of words. Space is also allowed between the words. But if a user will enter space/hyphen/underscore alone then it shouldn't accept.
  i.e,</p>
  <pre><code>var a = &quot;abc_def&quot; //allowed
  var b = &quot;abc def&quot; //allowed
  var c = &quot;abc-def&quot; //allowed
  var d = &quot;abc*def&quot; //not allowed, all other special characters are also not allowed
  var e = &quot; &quot; // not allowed
  var f = &quot;_&quot; // not allowed
  var g = &quot;-&quot; // not allowed
  </code></pre>
  <p>I wrote below regex pattern</p>
  <pre><code>^(([A-Za-z0-9])+([\-|_])?)+$
  </code></pre>
  <p>but it's not accepting space if entered in between the words. It should accept as I wrote in above 7 scenarios. How can I rectify the issue?</p>