匹配具有唯一数字的字母数字单词(NOT NUMERIC-ONLY单词)

Man*_*oey 7 javascript regex

使用正则表达式,我想只选择以下单词:

  • 是字母数字
  • 不包含数字
  • 不包含字母表
  • 有唯一的数字(1或更多)

我对正则表达式并不是很好,但到目前为止,我已经尝试过[^\d\s]*(\d+)(?!.*\1),这使我无法接近所需的输出:(

以下是输入字符串:

I would like abc123 to match but not 123.
ab12s should also match
Only number-words like 1234 should not match
Words containing same numbers like ab22s should not match
234 should not match
hel1lo2haha3hoho4
hel1lo2haha3hoho3
Run Code Online (Sandbox Code Playgroud)

预期比赛:

abc123
ab12s
hel1lo2haha3hoho4
Run Code Online (Sandbox Code Playgroud)

Cer*_*nce 8

您可以使用

\b(?=\d*[a-z])(?=[a-z]*\d)(?:[a-z]|(\d)(?!\w*\1))+\b
Run Code Online (Sandbox Code Playgroud)

https://regex101.com/r/TimjdW/3

用词边界锚定模式的开始和结束\b,然后:

  • (?=\d*[a-z]) - 在单词的某处预测字母字符
  • (?=[a-z]*\d) - 在单词的某处找一个数字的前瞻
  • (?:[a-z]|(\d)(?!\w*\1))+ 反复匹配:
    • [a-z] - 任何字母字符,或
    • (\d)(?!\w*\1) - 在同一个单词中不会再出现的数字