继续求教js正则表达式问题~~高分求~~谢谢~

tinerli 2011-10-08 04:48:45
有这么个需求:
命名必需以非'-'和'_'开头,以字母、数字和'-'和'_'组成,末尾不能是'_'或者'_'加数字,并且不能以纯数字命名。

怎么来写,写了半天也搞不对了,头大了。。。。。

...全文
183 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
打油的程序员 2011-10-09
  • 打赏
  • 举报
回复

//上面的java代码,你可能想问我为什么这么写,下面给出解题步骤
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestRegex {
public static void main(String[] args) {
String[] sourceStrings = {
"-test","_test","test_","test_123","test-","test-123","123"//这是你给出的错误例子
,"test","test_test123","test-test123"//这是你给出的正确例子
,"1test2","te1st","1test2","test2"//不存在-_时,至少有一个字母
,"1te-st2A","te-1stA","1te-st2A","te-st2A"//存在-_时(不能在两头),且结尾是字母时
,"1te-st2","te-1st2","1te-st2","te-st2"//存在-_时(不能在两头),且结尾是数字时
};//不存在-_时,至少有一个字母

String regex = "^[a-zA-Z\\d]+[a-zA-Z][a-zA-Z\\d]*$";//不存在-_时,至少有一个字母
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[a-zA-Z]$";//存在-_时(不能在两头),且结尾是字母时
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$";//存在-_时(不能在两头),且结尾是数字时

//以字母、数字和'-'和'_'组成,
//命名必需以非'-'和'_'开头,
//末尾不能是'_'或者'_'加数字,末尾不能是‘-’或者‘-’加数字,
//不能以纯数字命名。

Pattern pattern = Pattern.compile(regex);

for(int i=0; i<sourceStrings.length; i++){
System.out.println("----------------------------------");
System.out.println(sourceStrings[i] + " 匹配吗? " );
System.out.println(pattern.matcher(sourceStrings[i]).find());
}
}
}
/*
----------------------------------
-test 匹配吗?
false
----------------------------------
_test 匹配吗?
false
----------------------------------
test_ 匹配吗?
false
----------------------------------
test_123 匹配吗?
false
----------------------------------
test- 匹配吗?
false
----------------------------------
test-123 匹配吗?
false
----------------------------------
123 匹配吗?
false
----------------------------------
test 匹配吗?
true
----------------------------------
test_test123 匹配吗?
true
----------------------------------
test-test123 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
te1st 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
test2 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-1stA 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-st2A 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-1st2 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-st2 匹配吗?
true
*/


打油的程序员 2011-10-09
  • 打赏
  • 举报
回复
这是详细的java代码

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestRegex {
public static void main(String[] args) {
String[] sourceStrings = {
"-test","_test","test_","test_123","test-","test-123","123"//这是你给出的错误例子
,"test","test_test123","test-test123"//这是你给出的正确例子
,"1test2","te1st","1test2","test2"//不存在-_时,至少有一个字母
,"1te-st2A","te-1stA","1te-st2A","te-st2A"//存在-_时(不能在两头),且结尾是字母时
,"1te-st2","te-1st2","1te-st2","te-st2"//存在-_时(不能在两头),且结尾是数字时
};//不存在-_时,至少有一个字母


String regex = "^[a-zA-Z\\d]+([a-zA-Z][a-zA-Z\\d]|[a-zA-Z\\d-_]*([a-zA-Z]|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]))$";
//以字母、数字和'-'和'_'组成,
//命名必需以非'-'和'_'开头,
//末尾不能是'_'或者'_'加数字,末尾不能是‘-’或者‘-’加数字,
//不能以纯数字命名。

Pattern pattern = Pattern.compile(regex);

for(int i=0; i<sourceStrings.length; i++){
System.out.println("----------------------------------");
System.out.println(sourceStrings[i] + " 匹配吗? " );
System.out.println(pattern.matcher(sourceStrings[i]).find());
}
}
}
/*output:
----------------------------------
-test 匹配吗?
false
----------------------------------
_test 匹配吗?
false
----------------------------------
test_ 匹配吗?
false
----------------------------------
test_123 匹配吗?
false
----------------------------------
test- 匹配吗?
false
----------------------------------
test-123 匹配吗?
false
----------------------------------
123 匹配吗?
false
----------------------------------
test 匹配吗?
true
----------------------------------
test_test123 匹配吗?
true
----------------------------------
test-test123 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
te1st 匹配吗?
true
----------------------------------
1test2 匹配吗?
true
----------------------------------
test2 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-1stA 匹配吗?
true
----------------------------------
1te-st2A 匹配吗?
true
----------------------------------
te-st2A 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-1st2 匹配吗?
true
----------------------------------
1te-st2 匹配吗?
true
----------------------------------
te-st2 匹配吗?
true
*/

打油的程序员 2011-10-09
  • 打赏
  • 举报
回复
我刚学javascript 语法还不懂,下面的java代码 ,可以对付第10楼的例子

regex = "^[a-zA-Z\\d]+([a-zA-Z][a-zA-Z\\d]|[a-zA-Z\\d-_]*([a-zA-Z]|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]))$";
打油的程序员 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 tinerli 的回复:]

非常感谢 这个是可以的!
[/Quote]



//虽然楼主已经结贴,但是另外一个贴的jshi123网友提出了 我的上面是不能匹配a123的,所以我将
//另一个贴的修改版也复制过来

//不好意思合并的时候 出了点误差,把*误看做是+了
//原始是这样的
regex = "^[a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*$";//不存在-_时,至少有一个字母
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[a-zA-Z]$";//存在-_时(不能在两头),且结尾是字母时
regex += "|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$";//存在-_时(不能在两头),且结尾是数字时

//合并后(现在的版本):
regex = "^[a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*$|^[a-zA-Z\\d]+[a-zA-Z\\d-_]*([a-zA-Z]$|[-_][a-zA-Z\\d]*[a-zA-Z][a-zA-Z\\d]*[\\d]$)";


峭沙 2011-10-09
  • 打赏
  • 举报
回复
<html>
<head>
</head>
<body>
<script>
var str = ['test','test_test123','test-test123', 'test123', '-test','_test','test_','test_123','test-','test-123','123'];
for(var i in str){
var result = /^\d*[A-Za-z]+\d*([\-\_][A-Za-z][A-Za-z\d]*)*$/.test(str[i]);
document.write(str[i]+': '+result+'<br/>');
}
</script>
</body>
</html>
上面的代码有点小错误,修正下,不过这段代码还是有点局限的,要非常全面的匹配需要用到或字符,不过你的需求好诡异,一般不都要求字母或_开头吗,哪有字母开头的。。
tinerli 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 k3108001263 的回复:]

Java code

//上面的java代码,你可能想问我为什么这么写,下面给出解题步骤
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestRegex {
public static……
[/Quote]
非常感谢 这个是可以的!
峭沙 2011-10-09
  • 打赏
  • 举报
回复
<html>
<head>
</head>
<body>
<script>
var str = ['test','test_test123','test-test123', '-test','_test','test_','test_123','test-','test-123','123'];
for(var i in str){
var result = /^[A-Za-z\d]*[A-Za-z]+([\-\_][A-Za-z][A-Za-z\d]*)*$/.test(str[i]);
document.write(str[i]+': '+result+'<br/>');
}
</script>
</body>
</html>
结果:
test: true
test_test123: true
test-test123: true
-test: false
_test: false
test_: false
test_123: false
test-: false
test-123: false
123: false
  • 打赏
  • 举报
回复

var rnd=/^[a-zA-Z\d][\w\-]*[a-zA-Z]$/;
rnd.test......
BLUE_LG 2011-10-08
  • 打赏
  • 举报
回复

var reg=/^[^_-]+(_[a-zA-Z0-9|-]*)*$/;
var str="t_3_-__123";
if(str.match(reg)){
var n=str.lastIndexOf("_");
var s=str.substring(n+1,str.length);
if(!s==parseInt(s))
alert("匹配")
else
alert("不匹配")
}
else{
alert("不匹配");
}
诺维斯基 2011-10-08
  • 打赏
  • 举报
回复
/^[^-_][a-zA-Z0-9]+([-_]+[a-zA-Z]+[\d]*)*$/
tinerli 2011-10-08
  • 打赏
  • 举报
回复
1楼描述的不够详细,大家看10#
tinerli 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cj205 的回复:]

末尾不能是'_'或者'_'加数字
这个是什么意思
[/Quote]
重新写下:命名必需以非'-'和'_'开头,以字母、数字和'-'和'_'组成,末尾不能是'_'或者'_'加数字,末尾不能是‘-’或者‘-’加数字,并且不能以纯数字命名。

比如我输入‘-test’,'_test',‘test_’,‘test_123’,‘test-’,‘test-123’,'123'都是不合法的
输入‘test’,'test_test123','test-test123'是合法的
Mr-Jee 2011-10-08
  • 打赏
  • 举报
回复
末尾不能是'_'或者'_'加数字
这个是什么意思
tinerli 2011-10-08
  • 打赏
  • 举报
回复
以字母、数字和'-'和'_'组成,末尾不能是'_'或者'_'加数字,并且不能以纯数字命名。
不能以纯数字指不能写1,或者100之类的,但是可以写成test100
Mr-Jee 2011-10-08
  • 打赏
  • 举报
回复
当然不过咯 因为你要求结尾不能是数字的。
tinerli 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cj205 的回复:]

/^[a-zA-Z\d][a-zA-Z\d-_]*[a-zA-Z]$/
用最直接的方式给你写的正则。因为结尾必须是字母(末尾不能是'_'或者'_'加数字)
你的纯数字命名已经可以排除了。
[/Quote]
兄台的这个表达式验证test100就不过呢。

tinerli 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sohighthesky 的回复:]

命名必需以非'-'和'_'开头,就自然不是纯数字了吧?
[/Quote]
字母数字开头都行~
sohighthesky 2011-10-08
  • 打赏
  • 举报
回复
以字母、数字和'-'和'_'组成,末尾不能是'_'或者'_'加数字,并且不能以纯数字命名。
/^[a-zA-Z\d][\w\-]*[a-zA-Z]$/
sohighthesky 2011-10-08
  • 打赏
  • 举报
回复
命名必需以非'-'和'_'开头,就自然不是纯数字了吧?
Mr-Jee 2011-10-08
  • 打赏
  • 举报
回复
/^[a-zA-Z\d][a-zA-Z\d-_]*[a-zA-Z]$/
用最直接的方式给你写的正则。因为结尾必须是字母(末尾不能是'_'或者'_'加数字)
你的纯数字命名已经可以排除了。
加载更多回复(1)

87,996

社区成员

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

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