473,411 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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
May 13 '07 #1
6 2376
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.
May 13 '07 #2
On May 12, 9:29 pm, Jeff <dont_bug...@all.ukwrote:
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])
}
May 13 '07 #3
scripts.contact wrote:
On May 12, 9:29 pm, Jeff <dont_bug...@all.ukwrote:
>>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
}

May 13 '07 #4
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.
May 13 '07 #6
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/
May 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Josip Maslać | last post by:
Hello. I'm stuck! :) I need a regular expr. that would match any expresion that doesn't start with some words (not letters - words!) For example - reg. expr. that matches any expression...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
0
by: Glenn Kidd | last post by:
I am trying express a regular expresion used for substitution that is causing me problems and I was hoping that someone might be able to help me. The first part of the regular expression is easy,...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
5
by: Josema | last post by:
Hi to all, i did this regular expresion <!--!.*!--> that matchs all occurrences of type <!--!Name!--> <!--!Description!--> and so on... I would like to know the regular expression to get...
2
by: intrader | last post by:
I can't figure out why VS2005 has a problem with the following definition Regex rx = new Regex(@"('|\")"); //here put squiggle under "); what I think I am writing is a regular expresion...
6
by: LEM | last post by:
Hi, I'm trying to use Regex to find a string inside a text buffer. I am trying to find this pattern: <B>1.33594 VAL</B> where 1.33594 is a number that may change. My problem is to find a...
7
by: PJ6 | last post by:
Is it possible for a regular expression to macth only an empty string? What I want is exactly {0}, but that causes the parser to throw an exception - parsing "{0}" - Quantifier {x,y} following...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.