连续数字转换为正则表达式?

cuishaoyuan 2011-09-30 02:56:50
例如有数字2001 2002 2003 2004 3001 3002 3003 3004 4201 4202 4203 4204 要转变为:200[1-4]|300[1-4]|420[1-4]

该怎么做?
...全文
177 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuishaoyuan 2011-10-10
  • 打赏
  • 举报
回复
谢谢!学习了![Quote=引用 14 楼 left_you 的回复:]
我写了几个代码,你看看吧................


HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function loadend(){
……
[/Quote]
Left_you 2011-10-09
  • 打赏
  • 举报
回复
我写了几个代码,你看看吧................


<!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>
  • 打赏
  • 举报
回复
正则不是只有一种写法
var a = [2001 ,2002, 2003 ,2004 ,3001 ,3002 ,3003 ,3004 ,4201 , 4202, 4203,4204];
var reg = new RegExp('^' + a.join('|') + '$');
Left_you 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cuishaoyuan 的回复:]

引用 1 楼 left_you 的回复:

循环,判断后一个减前一个数是不是1。



我试了下循环,没有解决。。能不能上个代码
[/Quote]

请问还需要吗?、、、要不我帮你试试
cuishaoyuan 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 blue_lg 的回复:]

不是什么都要正则的吧,这个题验证倒是可以用正则。。。
剩下的就循环实现吧。。。
[/Quote]

请问具体怎么写
cuishaoyuan 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 left_you 的回复:]

循环,判断后一个减前一个数是不是1。
[/Quote]


我试了下循环,没有解决。。能不能上个代码
licip 2011-10-05
  • 打赏
  • 举报
回复
感觉关用正则应该还不行吧。
cuixiping 2011-10-05
  • 打赏
  • 举报
回复
除了循环,还能怎么样?
BLUE_LG 2011-10-03
  • 打赏
  • 举报
回复
不是什么都要正则的吧,这个题验证倒是可以用正则。。。
剩下的就循环实现吧。。。
诺维斯基 2011-10-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cj205 的回复:]
问个问题啊
2009 2010 2011 2012 3001 3002 3003 3004 4201 4202 4203 4204

这个怎么办?
[/Quote]
问的好!!
Mr-Jee 2011-09-30
  • 打赏
  • 举报
回复
问个问题啊
2009 2010 2011 2012 3001 3002 3003 3004 4201 4202 4203 4204

这个怎么办?
峭沙 2011-09-30
  • 打赏
  • 举报
回复
正则还没强大到无所不能。。你的数字是放在哪的?数组?
Left_you 2011-09-30
  • 打赏
  • 举报
回复
循环,判断后一个减前一个数是不是1。

87,989

社区成员

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

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