Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Javascript / Client Side Development

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

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

http://www.hunlock.com/blogs/An_Introduction_to_Regular_Expressions_i...

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

http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/g...

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.

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/JavaScript/Regular-expressions-in-Java...
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])
  }

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

On May 12, 9:50 pm, -Lost <maventheextrawo@techie.com> wrote:

And:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/scri...

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.

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/

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc