Javascript不解析嵌套的Bbcode
我编写了一个Javascript代码,类似于我用来编写此消息的代码。它还集成了一个实时预览框,就像我在下面看到的一样。目前我面临的唯一问题是一些嵌套的bbcode没有进行解析Javascript不解析嵌套的Bbcode,javascript,regex,bbcode,Javascript,Regex,Bbcode,我编写了一个Javascript代码,类似于我用来编写此消息的代码。它还集成了一个实时预览框,就像我在下面看到的一样。目前我面临的唯一问题是一些嵌套的bbcode没有进行解析 \[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\] 例如: [quote] [quote][/quote] [/quote] 语法分析不正确 这是我目前的Javascript function preview() { var txt = $('#editor').v
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
例如:
[quote]
[quote][/quote]
[/quote]
语法分析不正确
这是我目前的Javascript
function preview() {
var txt = $('#editor').val();
txt = txt.replace(/</g,'<');
txt = txt.replace(/>/g,'>');
txt = txt.replace(/[\r\n]/g,'%lb%');
var find = [
/\[quote\](.*?)\[\/quote\]/gi,
/\[quote author="(.*?)" date="(.*?)"\](.*?)\[\/quote\]/gi,
/\[b\](.*?)\[\/b\]/gi,
/\[i\](.*?)\[\/i\]/gi,
/\[u\](.*?)\[\/u\]/gi,
/\[left\](.*?)\[\/left\]/gi,
/\[center\](.*?)\[\/center\]/gi,
/\[right\](.*?)\[\/right\]/gi,
/\[size=(10|12|24|30)](.*?)\[\/size\]/gi,
/\[font=(.*?)](.*?)\[\/font\]/gi,
/\[color=(.*?)](.*?)\[\/color\]/gi,
/\[url(?:\=?)(.*?)\](.*?)\[\/url\]/gi,
/\[email=(.*?)\](.*?)\[\/email\]/gi,
/\[email\](.*?)\[\/email\]/gi,
/\[img(.*?)\](.*?)\[\/img\]/gi,
/(?:%lb%|\s)*\[code(?:\=?)(?:.*?)\](?:%lb%|\s)*(.*?)(?:%lb%|\s)*\[\/code\](?:%lb%|\s)*/gi,
/\[list(.*?)\](.*?)\[\*\](.*?)(?:%lb%|\s)*(\[\*\].*?\[\/list\]|\[\/list\])/i,
/(?:%lb%|\s)*\[list\](?:%lb%|\s)*(.*?)(?:%lb%|\s)*\[\/list\](?:%lb%|\s)*/gi,
/(?:%lb%|\s)*\[list=(\d)\](?:%lb%|\s)*(.*?)(?:%lb%|\s)*\[\/list\](?:%lb%|\s)*/gi,
/(?:%lb%){3,}/g
];
var replace = [
'<blockquote><div class="quote"><div class="quote_body">$1</div></div></blockquote>',
'<blockquote><div class="quote"><div class="quote_author"><span class="quote_from">Quote from</span> <span class="author">$1</span> on <span class="date">$2</span></div><div class="quote_body">$3</div></div></blockquote>',
'<b>$1<\/b>',
'<i>$1<\/i>',
'<u>$1<\/u>',
'<div class="align_left">$1<\/div>',
'<div class="align_center">$1<\/div>',
'<div class="align_right">$1<\/div>',
'<span style="font-size:$1px;">$2</span>',
'<span style="font-family:$1;">$2</span>',
'<span style="color:$1;">$2</span>',
'<a href="$1">$2</a>',
'<a href="mailto:$1">$2</a>',
'<a href="mailto:$1">$1</a>',
'<img $1 src="$2" />',
'<pre><code>$1</code></pre>',
'[list$1]$2<li>$3</li>$4',
'<ul>$1</ul>',
'<ol start=$1>$2</ol>',
'%lb%%lb%'
];
// fix [*] so that they only work inside [/list]
for(var i in find)
{
txt = txt.replace(find[i],replace[i]);
if(i == 17) while(txt.match(find[i],replace[i])) txt = txt.replace(find[i],replace[i]);
}
// Fix Smilies
txt = txt.replace(/%lb%/g,'<br />');
txt = txt.replace(/\:\)/g, '<img class="smiley" src="/img/smilies/smile.gif">');
txt = txt.replace(/\:-\)/g, '<img class="smiley" src="/img/smilies/happy.gif">');
txt = txt.replace(/\:D/g, '<img class="smiley" src="/img/smilies/biggrin.gif">');
txt = txt.replace(/\:\(/g, '<img class="smiley" src="/img/smilies/sad.gif">');
txt = txt.replace(/8\)/g, '<img class="smiley" src="/img/smilies/cool.gif">');
txt = txt.replace(/=O/g, '<img class="smiley" src="/img/smilies/surprised.gif">');
txt = txt.replace(/\:-\|\|/g, '<img class="smiley" src="/img/smilies/mad.gif">');
txt = txt.replace(/\:P/g, '<img class="smiley" src="/img/smilies/stongue.gif">');
txt = txt.replace(/\}\-\(/g, '<img class="smiley" src="/img/smilies/confused.gif">');
// Format Dates
txt = txt.replace(/\d{10}/g, function($0) {
var d = new Date($0*1000);
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
return "" + months[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() + ", " + (d.getHours()%12) + ":" + d.getMinutes() + " " + (d.getHours()<12 ? 'AM' : 'PM');
});
// Update the preview box
$('.preview').html(txt);
}
',
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
“[列表$1]$2\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
“- $1
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
'$2',
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
“%lb%%lb%”
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
];
//修正[*],使它们只在[/list]内工作
for(查找中的变量i)
{
txt=txt.replace(查找[i],替换[i]);
如果(i==17)while(txt.match(find[i],replace[i]))txt=txt.replace(find[i],replace[i]);
}
//修复微笑
txt=txt.replace(/%lb%/g,);
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/\:\)/g',);
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/\:-\)/g',);
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/\:D/g',);
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/\:\(/g');
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/8\)/g',);
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/=O/g');
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/\:-\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/\:P/g',);
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
txt=txt.replace(/\}-\(/g');
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
//格式日期
txt=txt.replace(/\d{10}/g,函数($0){
var d=新日期($0*1000);
var months=新数组(“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”);
\[quote\]((?:[^](?!\[quote\]))*?)\[\/quote\]
返回“+months[d.getMonth()]”+“+d.getDate()+”,“+d.getFullYear()+”,“+(d.getHours()%12)+”:“+d.getMinutes()+”+(d.getHours()三种解决方案:
您的BBCode不是常规语言,因此无法使用正则表达式进行解析。请编写一个解析器,跟踪打开和关闭的标记,以找到匹配的标记对。这样,您甚至可以发现语法错误(嵌套错误、缺少打开/关闭标记等)。HTML也是如此:您的BBCode不是常规语言,因此无法使用正则表达式进行解析。请编写一个解析器,跟踪打开和关闭的标记,以找到匹配的标记对。这样,您甚至可以发现语法错误(嵌套错误、缺少打开/关闭标记等)。HTML也是如此:我强烈建议#2:现有库可能会比您自己的库少一些问题,至少在第一个地方。我强烈建议#2:现有库可能会比您自己的库少一些问题,至少在第一个地方。