最后15分,怪事求助,谁能帮解决?

aspme 2006-12-31 11:57:07
请看我的ubb转html代码:
<script language=javascript>
function ubb2html()
{
var myhtm;
var re;
myhtm=theform.Content.value.toLowerCase();
//斜体字
myhtm=myhtm.replace(/\[i\](.[^\[]*)\[\/i\]/g,"<i>$1</i>");
//粗体字
myhtm=myhtm.replace(/\[b\](.[^\[]*)\[\/b\]/g,"<b>$1</b>");
document.write(myhtm)
}
</script>

怪事发生:当我的ubb代码是:asdf时,输出正常,但是当ubb代码是:asd是,输出:<b>asd</b> ,就是说斜体并没有转换,如果我把 ubb2html()中斜体代码放在粗体代码的下面时,情况刚好相反,请各位高手帮忙解答并解决,谢谢!
...全文
141 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbhbhbhbhb1021 2006-12-31
  • 打赏
  • 举报
回复
<script language=javascript>
function ubb2html()
{
var myhtm;
var re;
myhtm="[face=fghfgh]asdasd[/face]"
//斜体字
re=/\[i\](?!(\[i\]))((.(?!(\[i\])))*)\[\/i\]/gi;
myhtm=myhtm.replace(re,"<i>$2</i>");
//粗体字
re=/\[b\](?!(\[b\]))((.(?!(\[b\])))*)\[\/b\]/gi;
myhtm=myhtm.replace(re,"<b>$2</b>");
alert(myhtm)
//字体
myhtm=myhtm.replace(/\[face=([^\[]*)(\](?!(\[face)))((.(?!(\[face)))*)\[\/face\]/gi,"<font face=$1>$4</font>");
alert(myhtm)
document.write(myhtm)
}
ubb2html()
</script>
aspme 2006-12-31
  • 打赏
  • 举报
回复
多谢楼上的大大,可是我为了各位看得方便,所以省略了别的代码,类似于加粗等我现在会了,但别的呢?比如颜色,连接等?

希望楼上的大大再举类似的例子,谢谢

//字体
myhtm=myhtm.replace(/\[face=(.[^\[]*)\](.[^\[]*)\[\/face\]/g,"<font face=$1>$2</font>");
//字体大小
myhtm=myhtm.replace(/\[size=(.[^\[]*)\](.[^\[]*)\[\/size\]/g,"<font style=font-size:$1px>$2</font>");
//字体颜色
myhtm=myhtm.replace(/\[color=(.[^\[]*)\](.[^\[]*)\[\/color\]/g,"<font style=color:$1>$2</font>");
//居中
re=/\[center\](.[^\[]*)\[\/center\]/g;
myhtm=myhtm.replace(re,"<div align='center'>$1</div>");
//右对齐
re=/\[right\](.[^\[]*)\[\/right\]/g;
myhtm=myhtm.replace(re,"<div align='right'>$1</div>");
re=/\[quote\](.[^\[]*)\[\/quote\]/g;
//引用
myhtm=myhtm.replace(re,"<table bgcolor='#E6FFE6' width='90%' style='border:1px #009900 dotted' align='center'><tr><td class='td1'>$1</td></tr></table>");
//飞行
re=/\[fly\](.*)\[\/fly\]/g;
myhtm=myhtm.replace(re,"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>");
//移动
re=/\[move\](.*)\[\/move\]/g;
myhtm=myhtm.replace(re,"<MARQUEE scrollamount=3>$1</marquee>");
//阴影
re=/\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/SHADOW]/g;
myhtm=myhtm.replace(re,"<table width=$1><tr><td style='filter:shadow(color=$2, strength=$3)'>$4</td></tr></table>");
//外发光
re=/\[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/GLOW]/g;
myhtm=myhtm.replace(re,"<table width=$1 ><tr><td style='filter:glow(color=$2, strength=$3)'>$4</td></tr></table>")
//超链接1
re=/\[URL\](.[^\[]*)\[\/URL\]/g;
myhtm=myhtm.replace(re,"<a href='$1' target=_blank>$1</a>");
//超链接2
re=/\[URL=(.[^\[]*)\](.[^\[]*)\[\/URL\]/g;
myhtm=myhtm.replace(re,"<a href='$1' target=_blank>$2</a>");
//电子邮件链接1
re=/\[EMAIL\]([\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+)\[\/EMAIL\]/g;
myhtm=myhtm.replace(re,"<a href='mailto:$1'>$1</a>");
//电子邮件链接2
re=/\[EMAIL=([\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+)\](.[^\[]*)\[\/EMAIL\]/g;
myhtm=myhtm.replace(re,"<a href='mailto:$1'>$4</a>");
//图片
re=/\[img\](.[^\[]*)\[\/img\]/g;
myhtm=myhtm.replace(re,"<a href='$1' target=_blank><img src='$1' border='0' alt='单击在新窗口浏览'></a>");
//表情图标
re=/:em(\d{1,}):/g;
myhtm=myhtm.replace(re,"<img src='http://xs.tzblog.com/item/emot/$1.gif' border='0'>");
hbhbhbhbhb1021 2006-12-31
  • 打赏
  • 举报
回复
<script language=javascript>
function ubb2html()
{
var myhtm;
var re;
myhtm="asda"
//斜体字
re=/\[i\](?!(\[i\]))((.(?!(\[i\])))*)\[\/i\]/gi;
myhtm=myhtm.replace(re,"<i>$2</i>");
//粗体字
re=/\[b\](?!(\[b\]))((.(?!(\[b\])))*)\[\/b\]/gi;
myhtm=myhtm.replace(re,"<b>$2</b>");
document.write(myhtm)
}
ubb2html()
</script>
aspme 2006-12-31
  • 打赏
  • 举报
回复
非常感谢hbhbhbhbhb1021(天外水火(我要多努力)) ( )

如果出现下面的嵌套情况的话,还是会出现问题:
[face=楷体_GB2312][face=黑体]dasadsdas[/face][/face]

输出:[face=楷体_GB2312]<u><i><b><face=黑体>dasadsdas[/face]</b></i></u></face>

为什么会这样?
aspme 2006-12-31
  • 打赏
  • 举报
回复
带参数的怎么写呢?比如:

//字体
myhtm=myhtm.replace(/\[face=(.[^\[]*)\](.[^\[]*)\[\/face\]/g,"<font face=$1>$2</font>");
//字体大小
myhtm=myhtm.replace(/\[size=(.[^\[]*)\](.[^\[]*)\[\/size\]/g,"<font style=font-size:$1px>$2</font>");
//字体颜色
myhtm=myhtm.replace(/\[color=(.[^\[]*)\](.[^\[]*)\[\/color\]/g,"<font style=color:$1>$2</font>");


//超链接2
re=/\[URL=(.[^\[]*)\](.[^\[]*)\[\/URL\]/g;
myhtm=myhtm.replace(re,"<a href='$1' target=_blank>$2</a>");

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧