View Full Version : check if string starts with more than 2 of a letter

05-31-2012, 02:56 PM

How do you check if a string starts with more than 2 in a row of "a"

"ab" //this is fine
"aab" // this bad
"aaaaaaaaaaaaaaaaaaaaaab" //this is bad

thanks for the help

05-31-2012, 03:07 PM
if (str.charAt(0) == "a" && str.charAt(1) == "a")alert("too many a's")

Philip M
05-31-2012, 03:28 PM
if (str.charAt(0) == "a" && str.charAt(1) == "a")alert("too many a's")

I image that he wants to detect two consecutive characters the same (ignoring case), not just two a's. But some words do start with letters the same, oolite, ooze, Llandudno, eel, eery, llama, aardvark and many others.

<script type = "text/javascript">

function twoConsecutiveChars(str) {
for (var i = 0; i < 1; i++) {
if (str.charAt(i).toLowerCase() == str.charAt(i+1).toLowerCase()) {
alert ("String starts with two " + str.charAt(i).toLowerCase() + " in a row");



"Utility is when you have one telephone, luxury is when you have two, opulance is when you have three, and paradise when you have none." - Doug Larson