regular expresion remember matches - Javascript

This is a discussion on regular expresion remember matches - Javascript ; I always have trouble with javascript regexes... I want to parse apart a string and remember the matches and use them elswhere. Say I have: var test_string='one_two-three'; In perl I would do this; if($test_string=~/(.*)_(.*)-(.*)/){ print $2; } I'm not sure ...

+ Reply to Thread
Results 1 to 7 of 7

regular expresion remember matches

  1. Default regular expresion remember matches

    I always have trouble with javascript regexes...

    I want to parse apart a string and remember the matches and use them
    elswhere.

    Say I have:

    var test_string='one_two-three';

    In perl I would do this;

    if($test_string=~/(.*)_(.*)-(.*)/){
    print $2;
    }

    I'm not sure how to do this in javascript. Does $1 only work in the
    replace function????

    Jeff

  2. Default Re: regular expresion remember matches

    Jeff wrote:
    > I always have trouble with javascript regexes...
    >
    > I want to parse apart a string and remember the matches and use them
    > elswhere.
    >
    > Say I have:
    >
    > var test_string='one_two-three';
    >
    > In perl I would do this;
    >
    > if($test_string=~/(.*)_(.*)-(.*)/){
    > print $2;
    > }
    >
    > I'm not sure how to do this in javascript. Does $1 only work in the
    > replace function????


    Um... I suck at Regular Expressions (always have). But maybe:

    http://www.hunlock.com/blogs/An_Intr..._in_Javascript

    http://www.regular-expressions.info/javascript.html

    http://devedge-temp.mozilla.org/libr...de/regexp.html

    http://www.evolt.org/node/36435

    ....can be of some assistance. Otherwise, just wait, someone will be
    alone momentarily with your regexp.

    Ah what the hell... I'll throw up a regular expression (which is sure to
    be corrected/flawed):

    var test_string = 'one_two-three';
    var _regexp = /(\w+)_(\w+)-(\w+)/;
    alert(test_string.replace(_regexp, '$2'))

    That is basically the exact same thing you have. I am sure regexp gurus
    can offer a better solution though, both in your example and mine.

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.

  3. Default Re: regular expresion remember matches

    On May 12, 9:29 pm, Jeff <dont_bug...@all.uk> wrote:
    > I want to parse apart a string and remember the matches and use them
    > elswhere.


    Use match method:
    http://www.devarticles.com/c/a/JavaS...-JavaScript/6/
    http://msdn2.microsoft.com/en-us/library/7df7sf4x.aspx



    > var test_string='one_two-three';
    > if($test_string=~/(.*)_(.*)-(.*)/){
    > print $2;
    > }
    >
    > I'm not sure how to do this in javascript.


    var test_string='one_two-three',x;
    if(x=test_string.match(/(.*)_(.*)-(.*)/)){
    alert(x[2])
    }



  4. Default Re: regular expresion remember matches

    scripts.contact wrote:

    > On May 12, 9:29 pm, Jeff <dont_bug...@all.uk> wrote:
    >
    >>I want to parse apart a string and remember the matches and use them
    >>elswhere.

    >
    >
    > Use match method:
    > http://www.devarticles.com/c/a/JavaS...-JavaScript/6/
    > http://msdn2.microsoft.com/en-us/library/7df7sf4x.aspx
    >
    >
    >
    >
    >>var test_string='one_two-three';
    >>if($test_string=~/(.*)_(.*)-(.*)/){
    >>print $2;
    >>}
    >>
    >>I'm not sure how to do this in javascript.

    >
    >
    > var test_string='one_two-three',x;


    What's with the ,x? Is that like doing this:

    var test_string='one_two-three';
    var x;

    > if(x=test_string.match(/(.*)_(.*)-(.*)/)){
    > alert(x[2])


    Ahhh! Thanks!

    OT. Is Martin Honnen still around?

    Jeff

    > }
    >
    >



  5. Default Re: regular expresion remember matches

    Jeff wrote:
    > OT. Is Martin Honnen still around?


    I don't know about right this moment, but yeah, he's around.

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.

  6. Default Re: regular expresion remember matches

    Jeff said the following on 5/13/2007 12:07 AM:
    > scripts.contact wrote:


    <snip>

    >>
    >> var test_string='one_two-three',x;

    >
    > What's with the ,x? Is that like doing this:
    >
    > var test_string='one_two-three';
    > var x;


    Yes, it is the same thing.

    >> if(x=test_string.match(/(.*)_(.*)-(.*)/)){
    >> alert(x[2])

    >
    > Ahhh! Thanks!
    >
    > OT. Is Martin Honnen still around?


    Yes, you just don't see him as much anymore as you used to.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

+ Reply to Thread

Similar Threads

  1. Regular Expresion Error?
    By Application Development in forum RUBY
    Replies: 4
    Last Post: 10-04-2007, 08:26 PM
  2. Extracting second or third matches using Regular Expressions
    By Application Development in forum ADO DAO RDO RDS
    Replies: 0
    Last Post: 09-25-2007, 09:00 AM
  3. Condition: Matches regular Expression
    By Application Development in forum Sharepoint
    Replies: 0
    Last Post: 08-20-2007, 04:22 PM
  4. Removing ampersand - regular expresion replace with javascript
    By Application Development in forum Javascript
    Replies: 1
    Last Post: 05-04-2006, 02:43 PM
  5. Split a file into two (matches and non-matches)
    By Application Development in forum awk
    Replies: 8
    Last Post: 08-29-2005, 05:33 AM