日期:2014-05-16  浏览次数:20401 次

用js怎么实现每隔几个字符,添加一个字符? 急急急
用js怎么实现每隔几个字符,添加一个字符?   急急急
如;0102030405,变成01a02a03a04a05a
或者变成这样的换行显示
01
02
03

------解决方案--------------------
用正则替换行不?

/(.{2})/g
替换为:$1a
或者替换为:$1\n


------解决方案--------------------
<script type= "text/javascript ">
function XReplace(str,length){
var reg = new RegExp( "(\\S{ " + length + "}) ", "g ");
return str.replace(reg, "$1a ");
}

alert(XReplace( "00010002 ",4))
</script>
------解决方案--------------------
function Xreplace(str,length,reversed)
{
var re = new RegExp( "\\d{1, "+length+ "} ", "g ");
ma = str.match(re);
if(reversed)ma.reverse();
return ma.join( "a ") + "a ";//最后面不要 "a " 就去掉( + "a ")
}
alert(Xreplace( "000100020003 ",4));
alert(Xreplace( "000100020003 ",4,true));
alert(Xreplace( "000100020003 ",4,false));