Results 1 to 3 of 3

Thread: Regex Puzzle

  1. #1
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default Regex Puzzle

    I have a slight problem. I have a string, in which I want to match /\\e\[([^m]+)m/g, and replace it with a value pulled out of an array, the index of which should be \1. Is this even possible? The problem is that String.replace() doesn't parse \# until it's actually called, and the value needs to be taken from the array before the call happens. For this reason I don't think it can be done. Can anyone suggest an alternative approach?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    I have a string, in which I want to match /\\e\[([^m]+)m/g, and replace it with a value pulled out of an array, the index of which should be \1.
    Just to clarify, Twey, you have a string that may contain one or more sequences in a string that would match \e[XXm where XX is a number[1], and you want to replace that entire sequence with the value from the array with the aforementioned numeric index?

    Is this even possible?
    Yes.

    The problem is that String.replace() doesn't parse \# until it's actually called, and the value needs to be taken from the array before the call happens.
    When using a string for the second argument, yes. It's simple, though, using a function argument:

    Code:
    string.replace(/\\e\[([^m]+)m/g, function(match, index) {
      return array[index];
    });
    Unfortunately, some browsers (such as IE5) won't support this, so you might need to take a completely different approach:

    Code:
    var pattern = /\\e\[([^m]+)m/g,
        result  = '',
        index   = 0,
        matches;
    
    while((matches = pattern.exec(string))) {
      result += string.substring(index, matches.index)
              + array[matches[1]];
      index   = pattern.lastIndex;
    }
    result += string.substring(index);
    Mike


    [1] I assume it's a number, otherwise an array is inappropriate and an object (literal) would be better (and might do anyway). Moreover, if it is a number, then [^m] could be replaced with \d.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Aha! Brilliance. Thank you, Mike, that was exactly what I was after
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •