RegExps

D

David

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.
 
R

RobG

David said:
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.

[...]
 
D

David

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
 
R

RobG

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top