www.webdeveloper.com
Results 1 to 6 of 6

Thread: match and regular expressions

  1. #1
    Join Date
    Aug 2007
    Posts
    3,767

    match and regular expressions

    I was recently parsing SVG data using JavaScript String.match, and came across a problem. I was taking out coordinates to use them in a canvas application, and tried to do this:
    Code:
    var r = /[mc]([+-]?\d+(\.\d+)?,?)+/i;
    "m12.6-52.1".match(r);
    As it's supposed to, this just matches the last capture of the bolded group, so, just -52.1 is returned. I can't think of an easy way around this, so I wondered if anyone else has one.

    If not, I'll probably end up leaving the regular expressions and using normal string methods to parse it, but I'd really like to use regular expressions.
    Great wit and madness are near allied, and fine a line their bounds divide.

  2. #2
    Join Date
    Sep 2009
    Posts
    93
    Why you use 'mc' insted of 'm' for matching letter 'm'.

    I really say I did not clearly undestand whcic h data matched

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    2 matches are returned: -52.1 and .1
    Can you be more specific in what should be returned?
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    Quote Originally Posted by Pprakash View Post
    Why you use 'mc' insted of 'm' for matching letter 'm'.
    Because I'm also matching for c! That's just a sample.

    @Fang, indeed I wasn't very clear. I want 12.6 and -52.1 without having to repeat myself in the regular expression.
    Code:
    /m([+-]?\d+(\.\d+)?,?)([+-]?\d+(\.\d+)?,?)/i
    will do what I want, but since I also want to be able to match a bezier curve which has 6 coordinates, I would rather not have to use things like this.
    Code:
    /c([+-]?\d+(\.\d+)?,?)([+-]?\d+(\.\d+)?,?)([+-]?\d+(\.\d+)?,?)([+-]?\d+(\.\d+)?,?)([+-]?\d+(\.\d+)?,?)([+-]?\d+(\.\d+)?,?)/i
    Great wit and madness are near allied, and fine a line their bounds divide.

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var r = /([+-]?[\d+\.?\d+]+)/g;
    var arr ="m12.6-52.1".match(r);
    alert(arr);
    At least 98% of internet users' DNA is identical to that of chimpanzees

  6. #6
    Join Date
    Aug 2007
    Posts
    3,767
    That'll do fine. I'd rather test for the m or c in the same regexp all in one go, but what you have is great, thanks.
    Great wit and madness are near allied, and fine a line their bounds divide.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles