#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Posts
    573
    Rep Power
    70

    Regex to find runs of two or more of the same character


    Hi

    I'm trying to create a Javascript regex pattern to find runs of two or more consecutive instances of any character. I Googled and found this: (.{2,})\1+ for Perl, but it doesn't do what I want. Here's what I would like to happen.

    Given the string: 'abcdeefghijjjjjjjklmnooooooooopqr1233334567789{{{{{stuvvvvvvvvwxyz'

    I'd like the match to return the strings
    ['ee', 'jjjjjjj', 'ooooooooo', '3333', '77', '{{{{{', 'vvvvvvvv']

    Is there a way to do this with Javascript regex patterns?

    Thanks for any help

    Edit: I found a pattern that works in BBEdit (.)\1{1,} but it doesn't work in Javascript.
    Last edited by ktoz; January 13th, 2014 at 04:00 PM.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,128
    Rep Power
    9398
    Don't know what you're doing but it works fine for me.
    Code:
    > "abcdeefghijjjjjjjklmnooooooooopqr1233334567789{{{{{stuvvvvvvvvwxyz".match(/(.)\1{1,}/g)
    ["ee", "jjjjjjj", "ooooooooo", "3333", "77", "{{{{{", "vvvvvvvv"]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Posts
    573
    Rep Power
    70
    Originally Posted by requinix
    Don't know what you're doing but it works fine for me.
    Code:
    > "abcdeefghijjjjjjjklmnooooooooopqr1233334567789{{{{{stuvvvvvvvvwxyz".match(/(.)\1{1,}/g)
    ["ee", "jjjjjjj", "ooooooooo", "3333", "77", "{{{{{", "vvvvvvvv"]
    It didn't work because I had it embedded in a capture group like so: /([a-z]|(.)\1{1,})/g

    I guess the next question is how to use parts of captured patterns to generate a replacement pattern, For example, I'd like to use the captured 'vvvvvvvv' string to generate a replacement string '_8_v_' where '8' is the length of the pattern and 'v' is the first character of the pattern.

    I tried several variations on the back reference, but either it can't be done, or I'm just not doing it right. Is it even possible to do something like the above? And if it is, how would it be done? This doesn't work, but conceptually, it shows what I want to do:

    somestring.replace(/(.)\1{1,}/g, '_' + \1.length + '_' + \1.charAt(0) + '_')
    Last edited by ktoz; January 13th, 2014 at 11:20 PM.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by ktoz
    It didn't work because I had it embedded in a capture group like so: /([a-z]|(.)\1{1,})/g
    If you want us to help you, then you need to give us the real, actual, unaltered code, not some "simplified" version you made up in an attempt to mimic the issue.

    Fantasy code only leads to fantasy answers.



    Originally Posted by ktoz
    I tried several variations on the back reference, but either it can't be done, or I'm just not doing it right. Is it even possible to do something like the above? And if it is, how would it be done?
    Read the manual.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo