Regex for allowing alphanumeric,-,_ and space

后端 未结 7 2256
北荒
北荒 2020-12-04 16:43

I am searching for a regular expression for allowing alphanumeric characters, -, _ or spaces in JavaScript/jQuery. I tried Googling but wasn\'t able to find that.

Ca

7条回答
  •  半阙折子戏
    2020-12-04 17:33

    For me I wanted a regex which supports a strings as preceding. Basically, the motive is to support some foreign countries postal format as it should be an alphanumeric with spaces allowed.

    1. ABC123
    2. ABC 123
    3. ABC123(space)
    4. ABC 123 (space)

    So I ended up by writing custom regex as below.

    /^([a-z]+[\s]*[0-9]+[\s]*)+$/i
    

    Here, I gave * in [\s]* as it is not mandatory to have a space. A postal code may or may not contains space in my case.

提交回复
热议问题