<script>var index=0
var code={}
var source='aaa[code type="Javascript"]<p>bsbs</p>[/code]bbb<div>这是Div内容</div>[code type="Asp"]hello world[/code]cccc…'
var target='aaa<Div id="a1" codetype="Javascript"><p>bsbs</p></Div>bbb<div>这是Div内容</div><Div id="a2" codetype="Asp">hello world</Div>cccc…'source=source.replace(/\[code\s+type\="([^\]]+)"\]/g,function($1,$2){
index++
code["a"+index]=$2
return '<Div id="a'+index+'" codetype="'+$2+'">'
}).replace(/\[\/code\]/g,"</Div>")alert(source)
alert(source===target) //true
alert(code.a1) //打印 Javascript
alert(code.a2) //打印 Asp
</script>
var code={}
var source='aaa[code type="Javascript"]<p>bsbs</p>[/code]bbb<div>这是Div内容</div>[code type="Asp"]hello world[/code]cccc…'
var target='aaa<Div id="a1" codetype="Javascript"><p>bsbs</p></Div>bbb<div>这是Div内容</div><Div id="a2" codetype="Asp">hello world</Div>cccc…'source=source.replace(/\[code\s+type\="([^\]]+)"\]/g,function($1,$2){
index++
code["a"+index]=$2
return '<Div id="a'+index+'" codetype="'+$2+'">'
}).replace(/\[\/code\]/g,"</Div>")alert(source)
alert(source===target) //true
alert(code.a1) //打印 Javascript
alert(code.a2) //打印 Asp
</script>
解决方案 »
- js 获取自动获取宽度
- JS动态为UL创建1个LI 为LI绑定一个Onlick事件。怎么做?
- javascript 替换字符 和 截取字符 同时进行怎么弄???
- 哪个高手能告诉我这个网站的验证是怎么做的??就是你输入的值与弹出的对话框里面的值进行比较验证
- javaScript保存Xml问题
- 关于修改form的enctype属性
- onload問題,在綫等,急!!
- 问题!!急!!!请看下我这段代码哪里有问题?
- 初学了3天的javascript,有个问题想问问大家,并送分88,祝福大家明年发,发,发,进来留言的都有分。
- jsp写的登陆验证界面总是出错,麻烦大家帮忙看看
- 层的位置问题(在线等,解决给分)
- 为什么所我的TXT未定义?
var i=0;
str = str.replace(/\[code type="(.*?)"\](.*?)\[\/code\]/gim,function(string,code,content){
i++;
codeList["a"+i] = code;
return '<Div id="a' +i+ '" codetype="' +code+ '">' +content+ '</Div>';
});不是很严格,有特殊情况再说,凑合着先
str = 'aaa[code type="Javascript"]<p>bsbs</p>[/code]bbb<div>这是Div内容</div>[code type="Asp"]hello world[/code]cccc…'
re=/(\[code type=\"\w*\"\])/ig
ct=str.match(re).length
for(i=1;i<=ct;i++)
str=str.replace(/(\[code type=(\"\w*\")\])([^\[\]]*)(\[\/code\])/,"<div id='a"+i+"' codetype=$2>$3<\/div>");
alert(str);
[code Type='asp']
有时候输入是:
[code Type="asp"]或[code type=asp]
甚至有时候全部是写。该如何处理?
==========================呵呵,是呀。现在应用需求有点变化了。另外上次你给我的那个处理[code]正则,我发现在,只要[code][/code]里存在<p>或</p>之类的,就不能读出来。一天下来都解决不了。
代码如下:
============<div id="test">
<P>以下是代码测试问题。</P>
<P>[code type="asp"]<p>asd</p>asdf<script>alert("酒店之星测试条款!");</script>[/code]</P>
<P> </P>
<P>大家看到没有?</P>
</Div>
<script>var s = document.getElementById("test").innerHTML;
var i = 0;function ReplaceMe(str)
{str=str.replace(/\[code\s+type\="([^\]]+)"\](.*?)\[\/code\]/ig,function($0,$1,$2){
i++;
return '<Div id="a'+i+'" codetype="'+$1+'" style="border:1px solid #000">'+$2+'</Div>'
})return str;
}
alert(ReplaceMe(s));
</script>
========================只要[code][/code]里出现<p>asd</p>就不能被替换。不知道为什么。
<P>以下是代码测试问题。</P>
<P>[code type="asp"]<p>asd</p>asdf<script>alert("酒店之星测试条款!");</script>[/code]</P>
<P> </P>
<P>大家看到没有?</P>
</Div>
<script>var s = document.getElementById("test").innerHTML;
var i = 0;function ReplaceMe(str)
{
var reg = /\[code\s+type=([\"|\']?)([^\]]+)\1]((.|(\r\n))*?)\[\/code\]/ig;
str=str.replace(reg, function(){
i++;
return '<Div id="a'+i+'" codetype="'+arguments[2]+'" style="border:1px solid #000">'+arguments[3]+'</Div>'
}); return str;
}
alert(ReplaceMe(s));
</script>
str = 'aaa[code type="Javascript"]<p>bsbs</p>[/code]bbb<div>这是Div内容</div>[code type=Asp]hello world[/code]cccc…[code type=\'Asp\']hello world[/code]cccc…'
re=/(\[code type=[\"|']?\w*[\"|']?\])/ig
ct=str.match(re).length
for(i=1;i<=ct;i++)
str=str.replace(/(\[code type=([\"|']?\w*[\"|']?)\])([^\[\]]*)(\[\/code\])/,"<div id='a"+i+"' codetype=$2>$3<\/div>");
alert(str);
没有考虑type="asp'的情况
如果程序里有ar[20]式的写法也会出现问题
str = 'aaa[code type="Javascript"]<p>bsbs</p>[/code]bbb<div>这是Div内容</div>[code type=Asp]hello world[/code]cccc…[code type=\'Asp\']hello world[/code]cccc…'
re=/(\[code type=[\"|']?\w*[\"|']?\])/ig
ct=str.match(re).length
for(i=1;i<=ct;i++)
str=str.replace(/(\[code type=[\"|']?(\w*)[\"|']?\])([^\[\]]*)(\[\/code\])/,"<div id='a"+i+"' codetype=\"$2\">$3<\/div>");
alert(str);
根本就不用考虑,一个未结束的字符串
str="type=\'asp\"";
alert(str);
不过应该没人想这么做的,呵呵!