[thelist] Regular expression for three or more consecutive occurrences of t he same character

Ben Gustafson Ben_Gustafson at lionbridge.com
Fri Feb 8 12:09:01 CST 2002


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
--
[ Picked text/plain from multipart/alternative ]
I have a little JavaScript function for checking whether a string has three
consecutive occurrences of the same character. I'm wondering if there's a
regular expression for this, one that could be changed to check for n number
of the same character in a row. How 'bout it, regexp wizards out there?

<tip type="JavaScript" author="Ben Gustafson">
Here's a little function for checking whether a string has three consecutive
occurrences of the same character. (Could get a little gnarly if you want to
check for many more):

function threeInARow(str)
{
	var j = 1, k = 2;
	for (var i = 0; i < str.length; i++)
	{
		if (str.charAt(i) == str.charAt(j) && str.charAt(j) ==
str.charAt(k))
			return true;
		j++;
		k++;
	}
	return false;
}

</tip>

--Ben



More information about the thelist mailing list