How to determine if a string contains a sequence of repeated letters

前端 未结 7 2029
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 16:01

Using JavaScript, I need to check if a given string contains a sequence of repeated letters, like this:

\"aaaaa\"

How can I do t

7条回答
  •  無奈伤痛
    2021-01-17 16:05

    Use regular expressions:

    var hasDuplicates = (/([a-z])\1/i).test(str)
    

    Or if you don't want to catch aA and the likes

    var hasDuplicates = (/([a-zA-Z])\1/).test(str)
    

    Or, if you've decided you want to clarify your question:

    var hasDuplicates = (/^([a-zA-Z])\1+$/).test(str)
    

提交回复
热议问题