RegExps

Discussion in 'Javascript' started by David, Jan 24, 2006.

  1. David

    David Guest

    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.
     
    David, Jan 24, 2006
    #1
    1. Advertising

  2. David

    RobG Guest

    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
     
    RobG, Jan 24, 2006
    #2
    1. Advertising

  3. David

    David Guest

    > 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
     
    David, Jan 24, 2006
    #3
  4. David

    David Guest

    Oh, btw thank you very much :)

    David
     
    David, Jan 24, 2006
    #4
  5. David

    RobG Guest

    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
     
    RobG, Jan 24, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fredrik Ramsberg

    Optimisation of regexps in Perl?

    Fredrik Ramsberg, Oct 14, 2003, in forum: Perl
    Replies:
    2
    Views:
    474
    Fredrik Ramsberg
    Oct 15, 2003
  2. Replies:
    4
    Views:
    572
  3. Spartanicus

    Help with Regexps wanted

    Spartanicus, Oct 21, 2004, in forum: HTML
    Replies:
    4
    Views:
    462
    William Park
    Oct 25, 2004
  4. Klaus Alexander Seistrup

    Expanding regexps

    Klaus Alexander Seistrup, Oct 28, 2003, in forum: Python
    Replies:
    5
    Views:
    389
    Klaus Alexander Seistrup
    Oct 29, 2003
  5. Kirk Strauser

    Problems with regexps

    Kirk Strauser, Nov 7, 2003, in forum: Python
    Replies:
    6
    Views:
    313
    Edward K. Ream
    Nov 8, 2003
Loading...

Share This Page