Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > RegExps

Reply
Thread Tools

RegExps

 
 
David
Guest
Posts: n/a
 
      01-24-2006
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.






 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      01-24-2006
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
 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      01-24-2006
> 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





 
Reply With Quote
 
David
Guest
Posts: n/a
 
      01-24-2006

Oh, btw thank you very much

David


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      01-24-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Examples of using "reluctant" subexpressions in regexps? david.karr@wamu.net Java 4 04-27-2005 07:46 PM
Help with Regexps wanted Spartanicus HTML 4 10-25-2004 05:11 PM
Problems with regexps Kirk Strauser Python 6 11-08-2003 04:39 AM
Expanding regexps Klaus Alexander Seistrup Python 5 10-29-2003 11:09 PM
Optimisation of regexps in Perl? Fredrik Ramsberg Perl 2 10-15-2003 08:30 AM



Advertisments