473,399 Members | 3,302 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,399 software developers and data experts.

RegExps

Hi, I'm trying to get a regExp to stop at the end of a css declaration in
the example below. I've tried many variations with no success. What I need
it to return is just the first element like this:

#myElement1 { display: block; width: 100%; padding: 5px 0 5px 10px; }

But it grabs both instead like this:

#myElement1 { display: block; width: 100%; padding: 5px 0 5px 10px; }
#myElement2 { display: block; width: 100%; padding: 5px 0 5px 10px; }
Does anyone know how to make it stop at the closing bracket of the first
style? Here is one example of the code I have tried.

<script type="text/javascript">
function alertReg(){
var element=document.getElementById("myDiv").innerHTML ;
var pattern= /\#myElement \{[^$]*\}/;
var patternMatch= element.match(pattern);
if(patternMatch){
alert(patternMatch);
}
}
</script>
In the HTML I have this div.

<div id="myDiv">
#myElement1 { display: block; width: 100%; padding: 5px 0 5px 10px; }
#myElement2 { display: block; width: 100%; padding: 5px 0 5px 10px; }
</div>
Any suggestions welcome.
David B.


Jan 24 '06 #1
4 1150
David wrote:
Hi, I'm trying to get a regExp to stop at the end of a css declaration in
the example below. I've tried many variations with no success. What I need
it to return is just the first element like this:

#myElement1 { display: block; width: 100%; padding: 5px 0 5px 10px; }

But it grabs both instead like this:

#myElement1 { display: block; width: 100%; padding: 5px 0 5px 10px; }
#myElement2 { display: block; width: 100%; padding: 5px 0 5px 10px; }
Does anyone know how to make it stop at the closing bracket of the first
style? Here is one example of the code I have tried.

<script type="text/javascript">
function alertReg(){
var element=document.getElementById("myDiv").innerHTML ;
var pattern= /\#myElement \{[^$]*\}/;
var pattern= /#myElement[^}]+}/;

will get everything from #myElement up to and including the first '}'
character. It will return:

#myElement1 { display: block; width: 100%; padding: 5px 0 5px 10px; }

var patternMatch= element.match(pattern);
if(patternMatch){
alert(patternMatch);

Match returns an array, so you want:

alert(patternMatch[0])
to get the first element explicitly. If you wanted all the #myElement
matches, use a g flag on the regular expression:

var pattern= new RegExp('#myElement[^}]+}','g');
and now you must use patternMatch[0] or you will see both of them again.

[...]

--
Rob
Jan 24 '06 #2
> var pattern= /#myElement[^}]+}/;

will get everything from #myElement up to and including the first '}'
character. It will return:

#myElement1 { display: block; width: 100%; padding: 5px 0 5px 10px; }

That works really well .. although I don't completely understand its logic.

Your telling it to start at ( /#myElement ) and then with the negated
character set, ( [^}]+ ) continue matching everything that is not a
closing bracket.. and keep doing this until finally it comes to the last
character defined which is the actual closing bracket ( }/; ).

I would never have thought this would work.

David

Jan 24 '06 #3

Oh, btw thank you very much :-)

David
Jan 24 '06 #4
David wrote:
[...]

Your telling it to start at ( /#myElement ) and then with the negated
character set, ( [^}]+ ) continue matching everything that is not a
closing bracket.. and keep doing this until finally it comes to the last
character defined which is the actual closing bracket ( }/; ).


Precisely. :-)
--
Rob
Jan 24 '06 #5

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

Similar topics

0
by: R. Tarazi | last post by:
Hello together, I'm having extreme difficulties using RegExps for a specific problem and would really appreciate any help and hope somebody will read through my "long" posting... 1. <?php...
5
by: Klaus Alexander Seistrup | last post by:
Hi, Is there a way to "expand" simple regexps? Something along the lines of: #v+ >>> rx = '(a|b)c?(d|f)' >>> expand_regexp(rx)
4
by: Magnus Lie Hetland | last post by:
Hi! I've been looking at ways of dealing with nested structures in regexps (becuase I figured that would be faster than the Python parsing code I've currently got) and came across a few...
4
by: possibilitybox | last post by:
I'm trying to make a unicode friendly regexp to grab sentences reasonably reliably for as many unicode languages as possible, focusing on european languages first, hence it'd be useful to be able...
2
by: Yorian | last post by:
I just started to try regexps in php and I didn't have too many problems, however I found a few when trying to build a templte engine. The first one is found is the dollar sign. In my template I...
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?
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
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...
0
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,...
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.