By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,407 Members | 2,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,407 IT Pros & Developers. It's quick & easy.

RegExps

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
> 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

P: n/a

Oh, btw thank you very much :-)

David
Jan 24 '06 #4

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.