4

code for detecting repeating letter in a string.

var str="paraven4sr";
var hasDuplicates = (/([a-zA-Z])\1+$/).test(str)        
alert("repeating string "+hasDuplicates);

I am getting "false" as output for the above string "paraven4sr". But this code works correctly for the strings like "paraaven4sr". i mean if the character repeated consecutively, code gives output as "TRUE". how to rewrite this code so that i ll get output as "TRUE" when the character repeats in a string

4 Answers 4

26

JSFIDDLE

var str="paraven4sr";
var hasDuplicates = (/([a-zA-Z]).*?\1/).test(str)        
alert("repeating string "+hasDuplicates);

The regular expression /([a-zA-Z])\1+$/ is looking for:

  • ([a-zA-Z]]) - A letter which it captures in the first group; then
  • \1+ - immediately following it one or more copies of that letter; then
  • $ - the end of the string.

Changing it to /([a-zA-Z]).*?\1/ instead searches for:

  • ([a-zA-Z]) - A letter which it captures in the first group; then
  • .*? - zero or more characters (the ? denotes as few as possible); until
  • \1 - it finds a repeat of the first matched character.

If you have a requirement that the second match must be at the end-of-the-string then you can add $ to the end of the regular expression but from your text description of what you wanted then this did not seem to be necessary.

10
  • 1
    If you want 3 matches, simply put \1\1, if you want 4 matches put \1\1\1 in the regex :)
    – Smile4ever
    Commented Apr 3, 2016 at 8:25
  • @ MT0 here is a question regarding something similar. If i have x="1001" and use x.match(/(\d)\1+/g) --> i get the "00" ... but if i use it without the global I get two matches "00" and "0"... can you explain that? Commented Sep 13, 2017 at 15:43
  • @carinlynchin See the documentation for match it is explained there. A global match does not return the capturing groups just the matched sub-string(s), a non-global match returns the matched sub-string and the capturing groups within that sub-string.
    – MT0
    Commented Sep 13, 2017 at 23:39
  • 1
    @RohìtJíndal Regular expression matches do not work like that; they start looking for the next match after the end of the previous match (rather than the behaviour you are hoping for of starting looking from the character after the start of the previous match and finding nested or overlapping matches).
    – MT0
    Commented Oct 20, 2022 at 7:34
  • 1
    @RohìtJíndal Use a zero-width match: findReapeatingLetters = value => Array.from(value.matchAll(/(?=(([a-zA-Z]).*?\2))/g), a => a[1]) then findReapeatingLetters("Cookbosoks") outputs ["oo", "okbo", "kbosok", "oso", "soks"]
    – MT0
    Commented Oct 20, 2022 at 8:29
3

Try this:

var str = "paraven4sr";
function checkDuplicate(str){
    for(var i = 0; i < str.length; i++){
        var re = new RegExp("[^"+ str[i] +"]","g");
        if(str.replace(re, "").length >= 2){
            return true;
        }
    }
    return false;
}
alert(checkDuplicate(str));

Here is jsfiddle

1
  • Good, What is "g" in this line " var re = new RegExp("[^"+ str[i] +"]","g"); " Commented Sep 8, 2015 at 6:00
1

To just test duplicate alphanumeric character (including underscore _):

console.log(/(\w)\1+/.test('aab'));
0

Something like this?

String.prototype.count=function(s1) { 
   return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
}

"aab".count("a") > 1

EDIT: Sorry, just read that you are not searching for a function to find whether a letter is found more than once but to find whether a letter is a duplicate. Anyway, I leave this function here, maybe it can help. Sorry ;)

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.