473,387 Members | 1,520 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,387 software developers and data experts.

[RegExp] Making non-greedy; Escaping parentheses?

Hello,

I need to browse a list of hyperlinks, each followed by an
author, and remove the links only for certain authors.

1. I searched the archives on Google, but didn't find how to tell the
RegExp object to be non-greedy as using the ? quantifier doesn't seem
to work.

--------- SAMPLE ----------------
var items = new Array("johndoe","janedoe"
// Add parentheses to match any item in items()
var list = '('
list += items.join("|")
list += ')'

//Example: <A href="dummy.php?page=10#934569">TITLE
</A>, AUTHOR, April 12, 2003<br>

pattern = '<A href=".+?#[0-9]+?">.+?</A>, '
pattern += list
pattern += ',.+?<br>'

var input = new RegExp(temp,"gi");
var output = 'TROLL<br>'
document.body.innerHTML = body.replace(input,output);
--------- SAMPLE ----------------

Does somebody know how to do this?

2. Also, I notice that when using (johndoe|janedoe) in a pattern, the
value is copied into one of the $x variables. In this particular case,
I don't need this.
Is there a way to escape parentheses to tell RegEx _not_ to put this
item into a variable? I tried "\(" and "((", to no avail.

Thank you very much for any help
JD.
Jul 20 '05 #1
3 9157


Jane Doe wrote:


2. Also, I notice that when using (johndoe|janedoe) in a pattern, the
value is copied into one of the $x variables. In this particular case,
I don't need this.
Is there a way to escape parentheses to tell RegEx _not_ to put this
item into a variable? I tried "\(" and "((", to no avail.


I think you are looking for non-capturing parentheses e.g.
/(?:john|jane)doe/
but that is only supported with IE5.5+ and Netscape 6+

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Jane Doe <ja******@acme.com> writes:
I need to browse a list of hyperlinks, each followed by an
author, and remove the links only for certain authors.

1. I searched the archives on Google, but didn't find how to tell the
RegExp object to be non-greedy as using the ? quantifier doesn't seem
to work.
It should, if the browser is sufficiently new. The improved regular
expressions (non-greedy +,*,? and {}, non capturing bracketsa and
lookahead) are part of Javascript 1.5 and ECMAScript, not the eariler
Javascript versions.
--------- SAMPLE ----------------
var items = new Array("johndoe","janedoe"
Missing end parenthesis (and semicolon! Always end your sentences
with a semicolon.).
// Add parentheses to match any item in items()
var list = '('
list += items.join("|")
list += ')'

//Example: <A href="dummy.php?page=10#934569">TITLE
</A>, AUTHOR, April 12, 2003<br>
Is the entire string always on one line?
As a stupid convention, the regular expression "." matches
all non-EOL characters, but there is no shorthand for matching
any character. If the text contains newlines, you may need to
change "." to, e.g., "[\s\S]".
pattern = '<A href=".+?#[0-9]+?">.+?</A>, '
pattern += list
pattern += ',.+?<br>'
If your code is inside a script tag, and not in an external file,
you should escape your "</"'s as "<\/". Most browsers are forgiving.
var input = new RegExp(temp,"gi");
Do you mean "pattern" instead of "temp"?
var output = 'TROLL<br>'
document.body.innerHTML = body.replace(input,output);
--------- SAMPLE ----------------

Does somebody know how to do this?
One problem is, that a minimal match will still be as early as possible.
If you have two entries in a row, and the second has an author on your
hit-list, it will find a match starting at the first "<A". It finds
the minimal match starting there, which includes both entries, so
both are replaced.
To avoid this, you can restrict the .'s so they can't match too far:

pattern = '<A href="[^"]+?#\\d+?">[^<]+?</A>, ';
pattern += list;
pattern += ',[^>]+?<br>';

This prevents matching further than we want it. If there are tags
inside the TITLE or in the date after the author name, then "[^<]"
isn't sufficient as a restriction.
2. Also, I notice that when using (johndoe|janedoe) in a pattern, the
value is copied into one of the $x variables. In this particular case,
I don't need this.
Is there a way to escape parentheses to tell RegEx _not_ to put this
item into a variable? I tried "\(" and "((", to no avail.


Yes.
(?: ... )
This pair of parentheses are purely grouping, and the match won't be
remembered.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
On 12 Sep 2003 18:35:18 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
It should, if the browser is sufficiently new. The improved regular
expressions (non-greedy +,*,? and {}, non capturing bracketsa and
lookahead) are part of Javascript 1.5 and ECMAScript, not the eariler
Javascript versions.


Thank you very much Martin and Lasse :-) Finally got it working thanks
to you. I didn't know non-greedy regexes were so recent in JS.

Thanks again
JD.
Jul 20 '05 #4

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

Similar topics

3
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
5
by: Syed Ali | last post by:
Hello, I am trying to create a regexp to express non letters and space. I tried using: var myreg = new RegExp (""); However, it is not working. Basically I want to allow only words with...
4
by: McKirahan | last post by:
How would I use a regular expression to remove all trailing Carriage Returns and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance. Also, are they any great references for learning...
10
by: Jeff Sandler | last post by:
I have a page that accepts input from many textboxes. Many of the textboxes are intended to accept dates and times, thus, I expect only digits to be entered. I originally tested using parseInt...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
6
by: Christoph | last post by:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be...
3
by: jgarrard | last post by:
Hi, I have an array of strings which are regular expressions in the PERL syntax (ie / / delimeters). I wish to create a RegExp in order to do some useful work, but am stuck for a way of...
6
by: Christian Sonne | last post by:
Long story short, I'm trying to find all ISBN-10 numbers in a multiline string (approximately 10 pages of a normal book), and as far as I can tell, the *correct* thing to match would be this:...
4
by: r | last post by:
Hello, It seems delimiters can cause trouble sometimes. Look at this : <script type="text/javascript"> function isDigit(s) { var DECIMAL = '\\.'; var exp = '/(^?0(' + DECIMAL
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.