473,785 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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.i nnerHTML = body.replace(in put,output);
--------- SAMPLE ----------------

Does somebody know how to do this?

2. Also, I notice that when using (johndoe|janedo e) 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 9174


Jane Doe wrote:


2. Also, I notice that when using (johndoe|janedo e) 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)do e/
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.i nnerHTML = body.replace(in put,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|janedo e) 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
20989
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 input a regular expression, enabling the administrator to ensure a file upload matches a certain pattern. For instance, supplying the string '.exe$|.com$|.bat$|.zip$|.doc$'
5
2355
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. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
5
2058
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 letters in a textfield, space is ok, but no special characters such as $%^ or numbers such as
4
24772
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 how to use Regular Expressions?
10
2215
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 and isNaN, but I'm not even sure that the results are as perfect as I need. I am expecting to use RegExp.test(string), but I'm not 100% sure about that, either. Here is a test program with a textbox that has a maxlength of 2 characters. The...
8
2034
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 string (when you don't enter any values) - this is wrong d+ expression does not match, for example "g24" string - this is also wrong www.regexplib.com test validator works fine for both cases, i.e. it is reporting "not match" for the...
6
1415
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 working correctly: if( this.value != '' ) { if( !( RegExp( '^{0,200}$' ).test( this.value ))) { alert( 'Information provided must not be more than 200 characters.' ); this.focus(); } }
3
1623
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 getting these strings into the RegExp object. The RegExp constructor seems to want two parameters - the non / delimited expression, and the global modifiers.
6
1399
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: ".*\D*(\d{10}|\d{9}X)\D*.*" (it should be noted that I've removed all '-'s in the string, because they have a tendency to be mixed into ISBN's) however, on my 3200+ amd64, running the following:
4
2538
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
10315
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
10147
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
10085
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
9947
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
6737
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
5379
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...
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.