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("不匹配"); }
var rnd=/^[a-zA-Z\d][\w\-]*[a-zA-Z]$/; rnd.test......
<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
<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>上面的代码有点小错误,修正下,不过这段代码还是有点局限的,要非常全面的匹配需要用到或字符,不过你的需求好诡异,一般不都要求字母或_开头吗,哪有字母开头的。。
用最直接的方式给你写的正则。因为结尾必须是字母(末尾不能是'_'或者'_'加数字)
你的纯数字命名已经可以排除了。
/^[a-zA-Z\d][\w\-]*[a-zA-Z]$/
不能以纯数字指不能写1,或者100之类的,但是可以写成test100
输入‘test’,'test_test123','test-test123'是合法的
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("不匹配");
}
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
*/
//上面的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
*/
var rnd=/^[a-zA-Z\d][\w\-]*[a-zA-Z]$/;
rnd.test......
<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
<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>上面的代码有点小错误,修正下,不过这段代码还是有点局限的,要非常全面的匹配需要用到或字符,不过你的需求好诡异,一般不都要求字母或_开头吗,哪有字母开头的。。
//虽然楼主已经结贴,但是另外一个贴的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]$)";