Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    regexp find and seperate custom tags in a string

    Hi
    I am working on a CMS system for myself, Im having a bit of trouble with some reg exp.
    Say i have a text string like this...
    Code:
    {{startcontent_categories_box}}blah blah blah<br />
    <br />
    $p</p>
    <table cellspacing="1" cellpadding="1" width="200" border="1">
        <tbody>
            <tr>
                <td>username</td>
                <td>password</td>
            </tr>
            <tr id="rowstart">
                <td style="width: 250px"><strong>$username</strong></td>
                <td style="width: 250px">$userpassword</td>
            </tr>
        </tbody>
    </table>
    <p><br />
    {{endcontent_categories_box}}
    what I want to be able to do is run a reg exp which will find things like this on the page for me and seperate the content name, the start and end tags and the content within them.
    content name would be 'categories_box'
    start tag would be {{startcontent_categories_box}}
    end tag would be {{endcontent_categories_box}}
    and the content within would be all the html between the 2 tags.

    Is this possible?

    I have been trying myself but I am useless when it comes to reg exp, what I tried was...
    Code:
    /{{startcontent_/\d{2,50}/}}(.*?){{endcontent_/\d{2,50}/}}$/mg
    If someone could help me with this it would be greatly appreciated

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    /\{\{startcontent_\w{2,50}\}\}(.*?)\{\{endcontent_\w{2,50}\}\}$/mg
    I don't test, I fix only what I see at first look. \d means 0-9 and escape use \ , I guess you want \w instead of \d

    regards
    Last edited by oesxyl; 04-22-2008 at 11:10 PM.

  • #3
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    am I right, the way Im trying to use it is...

    var re = new RegExp("\{\{startcontent_\w{2,50}\}\}(.*?)\{\{endcontent_\w{2,50}\}\}$/mg");
    var aConholders = FCK.EditorDocument.body.innerText.match( re ) ;

    is that right?

    as I couldn't get it to work

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Chippo View Post
    am I right, the way Im trying to use it is...

    var re = new RegExp("\{\{startcontent_\w{2,50}\}\}(.*?)\{\{endcontent_\w{2,50}\}\}$/mg");
    var aConholders = FCK.EditorDocument.body.innerText.match( re ) ;

    is that right?

    as I couldn't get it to work
    yes, but aConholders is a Array not a String.

    http://www.javascriptkit.com/jsref/regexp.shtml

    Code:
    var re = new RegExp("\{\{startcontent_(\w{2,50})\}\}(.*?)\{\{endcontent_\1\}\}$/mg");
    var aConholders = FCK.EditorDocument.body.innerText.match( re ) ;
    if you modify this way you will probably get 'categories_box' in aConholders[0] and the content in aConholders[1].

    Edit: What is FCK.EditorDocument ?
    try to debug using alert

    regards
    Last edited by oesxyl; 04-22-2008 at 11:54 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by oesxyl View Post
    Code:
    /\{\{startcontent_\w{2,50}\}\}(.*?)\{\{endcontent_\w{2,50}\}\}$/mg
    I don't test, I fix only what I see at first look. \d means 0-9 and escape use \ , I guess you want \w instead of \d

    regards
    $/mg should be $/gi - there is no /m switch.

  • #6
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it still isnt actually returning any results, the whole value of FCK.EditorDocument.body.innerText is...
    Code:
    <p>homepage<br />
    <br />
    {{startcontent_categories_box}}blah blah blah<br />
    <br />
    $p</p>
    <table cellspacing="1" cellpadding="1" width="200" border="1">
        <tbody>
            <tr>
                <td>username</td>
                <td>password</td>
            </tr>
            <tr id="rowstart">
                <td style="width: 250px"><strong>$username</strong></td>
                <td style="width: 250px">$userpassword</td>
            </tr>
        </tbody>
    </table>
    <p><br />
    {{endcontent_categories_box}}<br />
    <br />
    {{startcontent_ecom_basket}}{{endcontent_ecom_basket}}<br />
    <br />
    &nbsp;</p>
    <p>[[ldfkjblnr]]</p>
    <p>&nbsp;</p>
    <p>[[sbmsfmb]]</p>
    <p>&nbsp;</p>
    dont know if that helps, and I have changed the regexp to what you suggested Philip M
    Code:
    var re = new RegExp("\{\{startcontent_\w{2,50}\}\}(.*?)\{\{endcontent_\w{2,50}\}\}$/gi");
    var aConholders = FCK.EditorDocument.body.innerText.match( re ) ;
    any ideas where I might be going wrong?

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I am afraid that the regex you were given was not correct. Try this:-

    var re = new RegExp(/\{\{startcontent_\w{2,50}\}\}[.\s\n\r]*)\{\{endcontent_\w{2,50}\}\}/i);
    var aConholders = FCK.EditorDocument.body.innerText.match( re ) ;

    alert (aConholders);

    Note that I have not tested this.

  • #8
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I get an error sayin "syntax error in regular expression" I tried changing [.\s\n\r]*) to ([.\s\n\e]*) as I noticed an close bracket with no open but that didnt work either, that just returns aContentholders as null

  • #9
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    its alrite actually guys, nice one for your help. Its definitly appreciated. Think i'll just make my cms work differently. Cheers anyway
    Last edited by Chippo; 04-23-2008 at 11:13 PM.

  • #10
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by Chippo View Post
    I get an error sayin "syntax error in regular expression" I tried changing [.\s\n\r]*) to ([.\s\n\e]*) as I noticed an close bracket with no open but that didnt work either, that just returns aContentholders as null
    in closing a paranthesis an r became an e on the same lines you could try


    Code:
    var t = '<p>homepage<br />\n<br />\n{{startcontent_categories_box}}blah blah blah<br />\n<br />\n$p</p>\n<table cellspacing="1" cellpadding="1" width="200" border="1">\n    <tbody>\n        <tr>\n            <td>username</td>\n            <td>password</td>\n        </tr>\n        <tr id="rowstart">\n            <td style="width: 250px"><strong>$username</strong></td>\n            <td style="width: 250px">$userpassword</td>\n        </tr>\n    </tbody>\n</table>\n<p><br />\n{{endcontent_categories_box}}<br />\n<br />\n{{startcontent_ecom_basket}}{{endcontent_ecom_basket}}<br />\n<br />\n&nbsp;</p>\n<p>[[ldfkjblnr]]</p>\n<p>&nbsp;</p>\n<p>[[sbmsfmb]]</p>\n<p>&nbsp;</p>';
    
    var m = /\{\{startcontent_(\w{2,50})\}\}((.|\s)*)\{\{endcontent_\1\}\}/g
          .exec(t);
    // m[1] contains categories_box
    // m[2] contains the text within
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    Chippo (04-24-2008)

  • #11
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    nice one mate, works perfectly. Thanx for all your help everyone


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •