Web Design and Development Forums

regexp find and seperate custom tags in a string

This is a discussion on "regexp find and seperate custom tags in a string" within the JavaScript Forum section. This forum, and the thread "regexp find and seperate custom tags in a string are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > JavaScript Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Apr 22nd, 2008, 22:14   #1 (permalink)
New Member
 
Join Date: Apr 2008
Location: Stockport, UK
Age: 21
Posts: 4
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: Select all
{{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: Select all
/{{startcontent_/\d{2,50}/}}(.*?){{endcontent_/\d{2,50}/}}$/mg
If someone could help me with this it would be greatly appreciated
Chippo is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 08:35   #2 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: regexp find and seperate custom tags in a string

. matches any character apart from new line and carriage return.
use this instead: [\s\S] - which means any whitespace and non-whitespace character (everything)

On a separate note, I know "g" is global and "i" is ignore case, but what's the "m" for? (in /mg at the end of the regexp)
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 14:09   #3 (permalink)
New Member
 
Join Date: Apr 2008
Location: Stockport, UK
Age: 21
Posts: 4
Re: regexp find and seperate custom tags in a string

Hi
I tried what you suggested but couldn't seem to get it to work where do you put the [\s\S]?

Code: Select all
var re = new RegExp("\{\{startcontent_\w{2,50}\}\}[\s\S]\{\{endcontent_\w{2,50}\}\}$/gi");
Chippo is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 14:59   #4 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: regexp find and seperate custom tags in a string

[\s\S] instead of .
...so....
/{{startcontent_/\d{2,50}/}}([\s\S]*?){{endcontent_/\d{2,50}/}}$/mg
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 17:06   #5 (permalink)
New Member
 
Join Date: Apr 2008
Location: Stockport, UK
Age: 21
Posts: 4
Re: regexp find and seperate custom tags in a string

Hi I tried what you said but still no result, someone else suggested this...
/\{\{startcontent_\w{2,50}\}\}([.\s\n\r]*)\{\{endcontent_\w{2,50}\}\}/i

but that didnt work either unfortunatly
Chippo is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 23rd, 2008, 22:13   #6 (permalink)
New Member
 
Join Date: Apr 2008
Location: Stockport, UK
Age: 21
Posts: 4
Re: regexp find and seperate custom tags in a string

its alrite actually mate, nice one for your help. Its definitly appreciated. Think i'll just make my cms work differently. Cheers anyway
Chippo is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 24th, 2008, 08:44   #7 (permalink)
Moderator
 
spinal007's Avatar
 
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,609
Blog Entries: 1
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: regexp find and seperate custom tags in a string

I have my own CMS and I use custom tags much more complicated than that - with properties and other rubbish. I can help you make this work.

Firstly, \s = all white space, including \r \n \t and so on. I can only assume whoever suggested you try that alternative doesn't have much experience with reg exp.

Secondly, you keep playing with your switches, but do you actually know what they mean? First you had, /mg, then you tried /i.
/i = ignore case
/g = global match (finds as many matches as possible)
/ig = ignore case & global match
I have never seen or heard of /m. I left the /m switch in my suggestion because I didn't know (and still don't know) what platform you're working on, but I guess /m may be the problem.

Problems I found in your regexp:
- \d will only matche DIGITS, not letters. it would match startcontent_1234 but not startcontent_hello.
- you had a / before the end }}, so you were actually searching for {{startcontent_1234/}}

So here's my revised suggestion:
- [\s\S]*? - lazy match anything between tags (this will work)
- escape all literal symbols such as {, }, / and _ (to make sure)
- /ig switch (to make sure)
- use \w instead of \d

Code: Select all
/\{\{startcontent\_/\w{2,50}\}\}([\s\S]*?)\{\{endcontent\_/\w{2,50}\}\}$/ig
__________________
Diego - SEO Consultant London (My Blog | Fight Me)
jQuery: Star Rating - Multiple File Upload - FCKEditor/Codepress
Before we work on artificial intelligence why don't we do something about natural stupidity?

Last edited by spinal007; Apr 24th, 2008 at 08:47.
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
RegExp.$1 Stormraven JavaScript Forum 1 Oct 1st, 2007 04:11
Changing Content On A Seperate Page josephman1988 Perl, Python, Ruby and Others 0 Sep 20th, 2007 13:42
a seperate domain for my forums(?)... prime Hosting & Domains 4 Aug 16th, 2006 15:05
Two Texts Aligned to Seperate Edges WiseWizards HTML Forum 4 Jul 23rd, 2005 06:36



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 19:05.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59