RegEx String, 12 signs (at least 1 number and at least 1 letter)

前端 未结 1 814
执念已碎
执念已碎 2021-01-14 11:08

I have problems with RegEx. How do I get an 12 signs long part of a string which contains at least 1 number and 1 letter?

Example: \"This is 12 signs long:

相关标签:
1条回答
  • 2021-01-14 11:17

    To find an alphanumeric word of length 12 within a longer text, use

    (?i)              # Case-insensitive matching
    \b                # Start of word
    (?=[A-Z]*[0-9])   # Assert presence of at least one ASCII digit
    (?=[0-9]*[A-Z])   # Assert presence of at least one ASCII letter
    [A-Z0-9]{12}      # Match exactly 12 ASCII letters/digits
    \b                # End of word
    

    or (for JavaScript, because it doesn't support verbose regexes)

    /\b(?=[A-Z]*[0-9])(?=[0-9]*[A-Z])[A-Z0-9]{12}\b/i
    
    0 讨论(0)
提交回复
热议问题