...

View Full Version : regexp find and seperate custom tags in a string



Chippo
04-22-2008, 10:56 PM
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...

{{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...

/{{startcontent_/\d{2,50}/}}(.*?){{endcontent_/\d{2,50}/}}$/mg

If someone could help me with this it would be greatly appreciated

oesxyl
04-22-2008, 11:06 PM
/\{\{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

Chippo
04-22-2008, 11:33 PM
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

oesxyl
04-22-2008, 11:49 PM
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



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].

What is FCK.EditorDocument ?
try to debug using alert

regards

Philip M
04-23-2008, 07:30 AM
/\{\{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.

Chippo
04-23-2008, 02:47 PM
it still isnt actually returning any results, the whole value of FCK.EditorDocument.body.innerText is...

<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

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?

Philip M
04-23-2008, 03:40 PM
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.

Chippo
04-23-2008, 04:09 PM
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

Chippo
04-23-2008, 09:19 PM
its alrite actually guys, nice one for your help. Its definitly appreciated. Think i'll just make my cms work differently. Cheers anyway

shyam
04-24-2008, 04:05 AM
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



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

Chippo
04-24-2008, 04:14 PM
nice one mate, works perfectly. Thanx for all your help everyone



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum