|
蓝森林 http://www.lslnet.com 2006年8月25日 8:28
ubb js 正则的问题
[code]
this.formatHtml=function(str){
// Clean up string
str=str.replace(/\[html\]/ig,"[html]");
str=str.replace(/\[\/html\]/ig,"[/html]");
str=str.replace(/\n\[html\]/ig,"[html]");
str=str.replace(/\n\[\/html\]/ig,"[/html]");
str=str.replace(/\[html\]\n/ig,"[html]");
str=str.replace(/\[\/html\]\n/ig,"[/html]");
while(str.indexOf("[html]", 0)>-1 && str.indexOf("[/html]", 0)>-1){
var intStart,intEnd;
var strText="";
var strSource=""
var strResult="";
intStart = str.indexOf("[html]", 0) + 6;
intEnd = str.indexOf("[/html]", intStart);
if(intEnd<=intStart) intEnd = intStart;
if(intEnd>intStart){
strText = str.substr(intStart, intEnd-intStart);
strResult = '<div class="code"><textarea id=code cols=45 rows=10>'+ this.encUBB(strText.replace(/^ +/gm," ").replace(/\:\/\//g,"://")) + "</textarea></div><br><input type='submit' name='Submit' value='Run Code'onclick=Preview() />";
}
intStart = str.indexOf("[html]", 0);
intEnd = str.indexOf("[/html]", intStart) + 7;
if(intEnd<=intStart+6) intEnd = intStart + 7;
strSource = str.substr(intStart, intEnd-intStart);
if(strResult!=""){
str = str.replace(strSource, strResult);
}else{
str = str.replace(strSource, strSource.replace(/\[/g, "["));
}
}
return str;
}
[/code]
这是一段ubb函数,我自己按照另外的一个格式写的,为什么不能正常转化呢? |
你写的太复杂了.....
我顺便学习一下正则:D
[code]
<script type="text/javascript">
function a(s){
var t=s.replace(/\[html\]([\s\S]*?)\[\/html\]/igm,function(){var s="<div class='code'><textarea id=code cols=45 rows=10>"+ arguments[1].replace(/[ \t]/img,' ').replace(/</img,'<').replace(/>/img,'>') + "</textarea></div><br><input type='submit' name='Submit' value='Run Code'onclick=Preview() />";return s;})
return t;
}
document.write(a("[html]<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n[/html]"))
</script>
[/code] |
|