javascript - Match the alphanumeric words(NOT NUMERIC-ONLY words) which have unique digits - Stack Overflow

Using regular expression, I want to select only the words which:are alphanumericdo not contain only nu

Using regular expression, I want to select only the words which:

  • are alphanumeric
  • do not contain only numbers
  • do not contain only alphabets
  • have unique numbers(1 or more)

I am not really good with the regex but so far, I have tried [^\d\s]*(\d+)(?!.*\1) which takes me nowhere close to the desired output :(

Here are the input strings:

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

Expected Matches:

abc123
ab12s
hel1lo2haha3hoho4

Using regular expression, I want to select only the words which:

  • are alphanumeric
  • do not contain only numbers
  • do not contain only alphabets
  • have unique numbers(1 or more)

I am not really good with the regex but so far, I have tried [^\d\s]*(\d+)(?!.*\1) which takes me nowhere close to the desired output :(

Here are the input strings:

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

Expected Matches:

abc123
ab12s
hel1lo2haha3hoho4
Share Improve this question edited Feb 2, 2019 at 6:58 CertainPerformance 372k55 gold badges352 silver badges357 bronze badges asked Feb 2, 2019 at 6:44 ManJoeyManJoey 2133 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

You can use

\b(?=\d*[a-z])(?=[a-z]*\d)(?:[a-z]|(\d)(?!\w*\1))+\b

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

Anchor the start and end of the pattern at word boundaries with \b, then:

  • (?=\d*[a-z]) - Lookahead for an alphabetical character somewhere in the word
  • (?=[a-z]*\d) - Lookahead for a digit somewhere in the word
  • (?:[a-z]|(\d)(?!\w*\1))+ Repeatedly match either:
    • [a-z] - Any alphabetical character, or
    • (\d)(?!\w*\1) - A digit which does not occur again in the same word

Here is a bit shorter & faster regex to make it happen since it doesn't assert negative lookahead for each character:

/\b(?=[a-z]*\d)(?=\d*[a-z])(?!\w*(\d)\w*\1)[a-z\d]+\b/ig

RegEx Demo

RegEx Details:

  • \b: Word boundary
  • (?=[a-z]*\d): Make sure we have at least a digit
  • (?=\d*[a-z]): Make sure we have at least a letter
  • (?!\w*(\d)\w*\1): Make sure digits are not repeated anywhere in the word
  • [a-z\d]+: Match 1+ alphanumericals
  • \b: Word boundary

You could assert all the conditions using one negative lookahead:

\b(?![a-z]+\b|\d+\b|\w*(\d)\w*\1)[a-z\d]+\b

See live demo here

The important parts are starting match from \b and immediately looking for the conditions:

  • [a-z]+\b Only alphabetic

  • \d+\b Only numeric

  • \w*(\d)\w*\1 Has a repeating digit

You can use this

\b(?!\w*(\d)\w*\1)(?=(?:[a-z]+\d+)|(?:\d+[a-z]+))[a-z0-9]+\b
  • \b - Word boundary.
  • (?!\w*(\d)\w*\1) - Condition to check unique digits.
  • (?=(?:[a-z]+\d+)|(?:\d+[a-z]+)) - Condition to check alphanumeric words.
  • [a-z0-9]+ - Matches a to z and 0 to 9

Demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744870644a4598241.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信