RegEx match exactly 4 digits

后端 未结 2 982
眼角桃花
眼角桃花 2021-01-04 00:10

Ok, i have a regex pattern like this /^([SW])\\w+([0-9]{4})$/

This pattern should match a string like SW0001 with SW-Prefix an

2条回答
  •  温柔的废话
    2021-01-04 00:26

    Let's see what the regex /^([SW])\w+([0-9]{4})$/ match

    1. Start with S or W since character class is used
    2. One or more alphanumeric character or underscore(\w = [a-zA-Z0-9_])
    3. Four digits

    This match more than just SW0001.

    Use the below regex.

    /^SW\d{4}$/
    

    This regex will match string that starts with SW followed by exactly four digits.

提交回复
热议问题