treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Select everything between curly brackets

  • I want to match any code that is between two curly brackets.

    lorem ipsum {loriipsu} lorem ipsum...

    I've got no further markup.
    I think it has to be done with :before and :after but I don't get it yet.
  • Within what languages are you using jquery? is this a form submission (php)? or you looking straight up xhtml/css?
  • I think you need to read more about regular expressions. Then create pattern of selecting the inner content of braces. If we talk about JavaScript it will look like:


    <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
    <html>
    <head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">
    <title>Untitled Document</title>
    <script type=\"text/javascript\">
    function myFunction () {
    var myText = document.getElementById('myTextArea').value;
    myText = myText.match(/{[^}]+}/g)
    alert (myText);
    }
    </script>
    </head>

    <body>
    <textarea id=\"myTextArea\">lorem ipsum {loriipsu} { lorem } ipsum...</textarea>
    <script type=\"text/javascript\">
    myFunction();
    </script>
    </body>
    </html>


    In a sample above /{[^}]+}/ is a pattern that selects everything inside of curly braces. g is activating the returning of array of found matches. Without it only the first match will be returned.