473,719 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex help please

Hi

Can anyone help me match this div below - my regex does not work - if
you could tell me why I would appreciate it.

var aStr = "<div class='feedflar e'>dfgdg dg</div>";
var reg = new RegExp("<div class='feedflar e'.*?</div>'","gim");
thanks
Tim
Aug 25 '08 #1
8 1462
pr
Tim Nash (aka TMN) wrote:
Can anyone help me match this div below - my regex does not work - if
you could tell me why I would appreciate it.

var aStr = "<div class='feedflar e'>dfgdg dg</div>";
var reg = new RegExp("<div class='feedflar e'.*?</div>'","gim");
-------------------------------------------------------^
That apostrophe shouldn't be there.

The 'm' flag is unnecessary.
Aug 25 '08 #2
After a fresh start this morning I got this to work taking into
account white spaces around 'class' and '=' etc and also
al/ow ' or " to be used

var reg = new RegExp("<div[^>]class\\s*=\
\s*[\'\"]feedflare[\'\"]*>(.*?)</div>", 'gi');

Tim
Aug 26 '08 #3
Tim Nash (aka TMN) wrote:
After a fresh start this morning I got this to work taking into
account white spaces around 'class' and '=' etc and also
al/ow ' or " to be used

var reg = new RegExp("<div[^>]class\\s*=\
\s*[\'\"]feedflare[\'\"]*>(.*?)</div>", 'gi');
Single-escaping the apostrophe within a double-quoted string literal is
useless ("\'" == "'"), and attr=['"]...['"]* is pointless (the star repeats
the previous expression zero or more times; here: ['"]). It would also be a
lot easier to maintain if you used a RegExp literal instead.

var reg = /<div[^>]class\s*=\s*['"]feedflare['"]>(.*?)<\/div>/gi;

That still does not exclude the possibility of e.g.

<divaclass="fee dflare'>...</div>

which is not Valid. As for the element type identifier followed by optional
attributes, you should use

<ident(|\s+attr ...)>

because whitespace after the identifier is required if there are attributes.
As for the matching quotes, you should use

('foo'|"foo")

However, RegExp literals and non-greedy matching (`.*?') are not universally
supported, with the latter being the more important fact here. See also:

<http://pointedears.de/scripts/es-matrix/>

Also note that a single regular expression cannot be used to parse an
*arbitrary* fragment of an SGML-based markup language; either it is too
greedy or not greedy enough. For example, in

<div class="foo"><di v>bar</div></div>

this non-greedy expression would match `<div class="foo"><di v>bar</div>'.
with the outer `div' element not being closed.

So, for reliable parsing, you will need to implement a push-down automaton;
however, its parsing algorithm can be made more efficient with regular
expressions.

Unsurprisingly, all this has been discussed here before. Please search
before you post.

<http://jibbering.com/faq/>
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 26 '08 #4
pr
Tim Nash (aka TMN) wrote:
After a fresh start this morning I got this to work taking into
account white spaces around 'class' and '=' etc and also
al/ow ' or " to be used

var reg = new RegExp("<div[^>]class\\s*=\
\s*[\'\"]feedflare[\'\"]*>(.*?)</div>", 'gi');
To match a string starting with any of the following common permutations:

<div class="feedflar e">
<div style="color: red;" class="feedflar e">
<div class="feedflar e" id="div1">
<div class="class1 feedflare class3">

you will instead need something like:

/<div\b[^>]+\bclass\s*=\s* (['"])[\w\s]*\bfeedflare\b[\w\s]*\1[^>]*>(.*?)<\/div\s*>/gi

I have simplified it by presuming you won't use the characters '.-:' in
class names. But as PointedEars points out, '.*?' is a problem in old
browsers and you're in trouble if there's a nested div in your string.

Possibly you would be better served by reading the string into the DOM
(using a DOMParser or innerHTML, for e.g.) and extracting information
from it there.
Aug 26 '08 #5
Thank you PointedEars and pr for your input.

Tim

pr wrote:
Tim Nash (aka TMN) wrote:
After a fresh start this morning I got this to work taking into
account white spaces around 'class' and '=' etc and also
al/ow ' or " to be used

var reg = new RegExp("<div[^>]class\\s*=\
\s*[\'\"]feedflare[\'\"]*>(.*?)</div>", 'gi');

To match a string starting with any of the following common permutations:

<div class="feedflar e">
<div style="color: red;" class="feedflar e">
<div class="feedflar e" id="div1">
<div class="class1 feedflare class3">

you will instead need something like:

/<div\b[^>]+\bclass\s*=\s* (['"])[\w\s]*\bfeedflare\b[\w\s]*\1[^>]*>(.*?)<\/div\s*>/gi

I have simplified it by presuming you won't use the characters '.-:' in
class names. But as PointedEars points out, '.*?' is a problem in old
browsers and you're in trouble if there's a nested div in your string.

Possibly you would be better served by reading the string into the DOM
(using a DOMParser or innerHTML, for e.g.) and extracting information
from it there.
Aug 26 '08 #6
pr
Thomas 'PointedEars' Lahn wrote:
As for the matching quotes, you should use

('foo'|"foo")
Or

(['"])foo\1
>
However, RegExp literals and non-greedy matching (`.*?') are not universally
supported, with the latter being the more important fact here.
Does this seem a reasonable feature test to you?

var ngq = /.+?/.exec("ab");
var hasNonGreedyQua ntifiers = ngq && ngq[0].length == 1;

I can only lay hands on one browser old enough to fail. I assume the
presence of literal notation, obviously.
Aug 26 '08 #7
pr wrote:
Thomas 'PointedEars' Lahn wrote:
> As for the matching quotes, you should use

('foo'|"foo")

Or

(['"])foo\1
Correct. To my surprise, this feature, standardized only with ECMAScript
Ed. 3 (like regular expressions in general), appears to be widely supported:

The bookmarklet

javascript:wind ow.alert(/^(["'])a\1b$/.test("'a'b"));

shows `true' in all my test environments, which currently are:

- Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.1)
Gecko/2008070208 Firefox/3.0.1
- Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14)
Gecko/20080404 Firefox/2.0.0.14
- Mozilla/4.78 [de] (Windows NT 5.0; U)

- Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE)
AppleWebKit/525.19 (KHTML, like Gecko) Version/3.1.2 Safari/525.21
- Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; de-de)
AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.22

- Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727) (IE 8 beta 1)
- Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0;
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 4.01; Windows NT 5.0; {...})

- Opera/9.52 (Windows NT 5.1; U; de)
- Opera/9.51 (Windows NT 5.1; U; de)
- Opera/9.27 (Windows NT 5.1; U; en)
- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.0
- Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.1)
Opera 7.02 [en]
>However, RegExp literals and non-greedy matching (`.*?') are not universally
supported, with the latter being the more important fact here.

Does this seem a reasonable feature test to you?

var ngq = /.+?/.exec("ab");
var hasNonGreedyQua ntifiers = ngq && ngq[0].length == 1;
No, it could already throw a (non-catchable) SyntaxError when /.+?/ is
parsed, before execution (you can test that with IE 5.0, for example). And
I have yet to devise a bullet-proof test for possibly unsupported syntax (a
more sophisticated application of eval() comes to mind), one that does not
break the ECMAScript program then.

However,

var ngq = null;

try
{
ngq = new RegExp(".+?");
}
catch (e)
{
}

if (nqg)
{
// ...
}

would work for script engines that support basic exception handling but not
non-greedy quantifiers (such as JScript 5.1 in IE 5.01; tested positive).
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 26 '08 #8
pr
Thomas 'PointedEars' Lahn wrote:
pr wrote:
>Does this seem a reasonable feature test to you?

var ngq = /.+?/.exec("ab");
var hasNonGreedyQua ntifiers = ngq && ngq[0].length == 1;

No, it could already throw a (non-catchable) SyntaxError when /.+?/ is
parsed, before execution (you can test that with IE 5.0, for example).
You're right. IE 5 reports "Unexpected quantifier".
Aug 27 '08 #9

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

Similar topics

4
9761
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
6
4797
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
5
29062
by: Greg Collins [InfoPath MVP] | last post by:
I couldn't find anything in my searches... I'm wondering if there's a Regex (with or without additional C# code) that can convert a either "lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with spaces). Thanx! -- Greg Collins Please visit: http://www.InfoPathDev.com
17
3973
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
9
2084
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat lot of times here.... Requirement: ------------------- I want to get the value of "href" i.e "city1.html" by searching "city1" between the <a</atag. Please note that "city1" can repeat lot of
10
3887
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program is using Python's standard re (regular expressions) module to parse the input file. As Python's routines to read from the file and regular expressions are most likely implemented via native libraries I would expect that the C code, which reads...
4
2184
by: Henrik Dahl | last post by:
Hello! In my application I have a need for using a regular expression now and then. Often the same regular expression must be used multiple times. For performance reasons I use the RegexOptions.Compiled when I instantiate it. It must be obvious that it takes some time to instantiate such an object. My question is, does the Regex instantiation somehow deal with some caching internally so instantiating a Regex object multiple times...
7
2063
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title Artist - Title (Remix) Artist - Title
6
4207
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character but not two or more? example myfile.doc = good My.file.doc = not good if you could give an example of the expression pattern that would most helpful. thanks phil
4
2139
by: seberino | last post by:
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris
0
8828
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
9208
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...
0
9053
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
7993
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
5971
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
4481
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
3185
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
2562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2124
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.