RegExps

Jan 24, 2006

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.

View 4 Replies


ADVERTISEMENT

RegExps : Grab Number Inside Of String

Oct 18, 2007

I'm looking for a way to grab a number from a string, although this number
is not at either end of the string.

Here is the string in question: var el = "v4Function_Name('argument3')"

Here is a substring that returns the number ( the 3 ) successfully...
var number = el.substring(el.lastIndexOf("ent")+3,el.length-2);

Seems very ineffecient to do it this way, and better to find a lean elegant
way using replace(); How does one go about getting a value inside of a
string with regular expressions? .. or is there a better way?

Here are a couple of the many expressions I have failed with...

var pat = /^[^ent][0-9]*$"/;
var pat = /v4Function_Name('argument([^0-9]+)'/;
var number = el.replace(pat,"");

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved