sandy
sandy

Reputation: 1155

Combine two regex condition using or

I have one regex to check below conditions in javascript

  1. at-least 1 number

  2. could be alphanumeric

  3. special character allowed would be - , space, #

    var regex= new RegExp (/^(?=.*\d)[a-zA-Z\d #-]+$/);
    

    This works fine.But I need to modify condition 1 as

    1. at-least 1 number or 1 character(2nd 3rd condition are unaltered)
      Is it possible to do it without using OR of regex.I even tried with OR attribute but found no luck.

Upvotes: 1

Views: 117

Answers (1)

Jerry
Jerry

Reputation: 71538

You can easily put it in a character class in the first requirement:

var regex= new RegExp (/^(?=.*[\dA-Za-z])[a-zA-Z\d #-]+$/);
                                 ^^^^^^

Upvotes: 5

Related Questions