Ildar Galikov Ildar Galikov - 2 months ago 7
Javascript Question

Regex test in JavaScript if a string contains only unique characters

A string contains only

[A-Za-z0-9]
characters. I need to know if the tested string contains at least one repeating character.

The following should return false:

abc1
abc


The following should return true:

abc11
1abc1
aabc1


abca

Answer

Use regex with a positive look ahead and capturing group.

/(?=^[A-Za-z0-9]+$)(.)+.*\1.*/

Regex explanation here

Regular expression visualization

Comments