How do I write the regular expression (letters, numbers and hyphen) correctly?

  • 0
    Hello everyone. Please tell me how to write a regular expression so that if the client entered something other than letters, numbers and hyphens in the field, the field would not be validated.
    JavaScript Anonymous, May 24, 2020

  • 1 Answers
  • 0
    if( /[^a-z0-9-]/.test(str) ){
    // валидация не пройдена
    }




    or so (but this option will still skip underscores)

    if( /[^\w-]/.test(str) ){
    // валидация не пройдена
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!