473,696 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='on e_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 2389
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='on e_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_stri ng.replace(_reg exp, '$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...@al l.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='on e_two-three';
if($test_string =~/(.*)_(.*)-(.*)/){
print $2;
}

I'm not sure how to do this in javascript.
var test_string='on e_two-three',x;
if(x=test_strin g.match(/(.*)_(.*)-(.*)/)){
alert(x[2])
}
May 13 '07 #3
scripts.contact wrote:
On May 12, 9:29 pm, Jeff <dont_bug...@al l.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='on e_two-three';
if($test_stri ng=~/(.*)_(.*)-(.*)/){
print $2;
}

I'm not sure how to do this in javascript.


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

var test_string='on e_two-three';
var x;
if(x=test_strin g.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='on e_two-three',x;

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

var test_string='on e_two-three';
var x;
Yes, it is the same thing.
> if(x=test_strin g.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.javas cript 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
1649
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 that doesn't start with words 'one' and 'two' ??
1
4172
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
0
1404
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, specifically to match "$!?*", but I do not want to match this if it is prefixed with the regular expression "Foo\.bar\(\s*". Also, on the matches I want to replace with "Foo\.bar\( $1 \)". Any help would be greatly appreciated. Thanks in...
11
5372
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, I want to use the text included within the brackets to do a lookup so that I can replace the expression with the new text. For example:
5
1837
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 (for instance) for this
2
1906
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 that will give me all the matches for single or double quotes and remember the match so that I can then say Regex.Replace(somestring,rx,@"\\$1");
6
1223
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 regular expression that can read that and extracts the number.
7
5892
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 nothing. Duh, I want nothing. Maybe I need an expression that evaluates to "not anything?" I can't find that either. Paul
14
4981
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 text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
8656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9140
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9005
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8869
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8845
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7693
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5848
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.