连续数字转换为正则表达式? 例如有数字2001 2002 2003 2004 3001 3002 3003 3004 4201 4202 4203 4204 要转变为:200[1-4]|300[1-4]|420[1-4]该怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 正则不是只有一种写法var a = [2001 ,2002, 2003 ,2004 ,3001 ,3002 ,3003 ,3004 ,4201 , 4202, 4203,4204];var reg = new RegExp('^' + a.join('|') + '$'); 我写了几个代码,你看看吧................<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <script type="text/javascript"> function loadend(){ var req = ""; var tempReq = ""; var flag = false; var array = [2001,2002,2003,2004,3001,3002,3003,3004,4201,4202,4203,4204,50,51,69,70]; for(i = 1;i < array.length;i ++){ //如果连续 if(array[i]-array[i-1] == 1){ //加那个“|“符号 if(req != "" && tempReq == "") tempReq = "|"; //如果十位上的数相同 if(Math.floor(array[i]/10) == Math.floor(array[i-1]/10)){ if(tempReq == "" || tempReq == "|"){ tempReq += Math.floor(array[i]/10) + "[" + array[i-1]%10; flag = true; } tempReq += array[i]%10; }else{ //4楼的问题我就没考虑了... } }else{ tempReq += "]"; req += tempReq; tempReq = ""; flag = false; } } if(flag){ tempReq += "]"; req += tempReq; } document.write(req); } </script> <title>MyHtml.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body onload="loadend();"> </body></html> 用chrome学习Javascript css 等 有什么优势 JS调用本地应用程序(*.exe)问题 求靠栏与不靠栏时不同大小的gadget代码.急~ FCKeditor使用问题 如何编写跨浏览器的javascript? 请问高手:父窗口执行子frame的全局函数,与子frame自己执行该函数结果不一样。 省市区连动问题----高分请教 有点难度:实现对输入的日期进行动态检查 有关事件的问题 怎样显示javascript函数的结果? 如何根据自定义属性,取元素? 有关js对象数组排序问题
var a = [2001 ,2002, 2003 ,2004 ,3001 ,3002 ,3003 ,3004 ,4201 , 4202, 4203,4204];
var reg = new RegExp('^' + a.join('|') + '$');
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function loadend(){
var req = "";
var tempReq = "";
var flag = false;
var array = [2001,2002,2003,2004,3001,3002,3003,3004,4201,4202,4203,4204,50,51,69,70];
for(i = 1;i < array.length;i ++){
//如果连续
if(array[i]-array[i-1] == 1){
//加那个“|“符号
if(req != "" && tempReq == "") tempReq = "|";
//如果十位上的数相同
if(Math.floor(array[i]/10) == Math.floor(array[i-1]/10)){
if(tempReq == "" || tempReq == "|"){
tempReq += Math.floor(array[i]/10) + "[" + array[i-1]%10;
flag = true;
}
tempReq += array[i]%10;
}else{
//4楼的问题我就没考虑了...
}
}else{
tempReq += "]";
req += tempReq;
tempReq = "";
flag = false;
}
}
if(flag){
tempReq += "]";
req += tempReq;
}
document.write(req);
}
</script>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<body onload="loadend();">
</body>
</html>