<SCRIPT Language="VBScript"> Function reg(Str) Dim Re,Match,Matches Set Re = New Regexp Re.Global = True Re.Ignorecase = True Re.Pattern="^\b[a-zA-Z0-9][a-zA-Z0-9_\-]+[a-zA-Z]\b$" Str=re.Replace(Str,"") If str = "" Then MsgBox "合法" Else MsgBox "不合法" End if End Function</SCRIPT><input type="text" id="sss" value=""><input type="button" value="ccc" onclick="reg(sss.value)">js我不是很熟悉,vbs的,测试过了,你放在页面中试试看吧
一个正则不行就多来几个 <SCRIPT Language="VBScript"> Function reg(Str) Dim Re,Match,Matches Set Re = New Regexp Re.Global = True Re.Ignorecase = True Re.Pattern="^\b[a-zA-Z0-9][a-zA-Z0-9_\-]+[a-zA-Z0-9]\b$" ck = re.Replace(Str,"") If ck = "" Then reg = "合法" Else reg = "不合法" Exit Function End If Re.Pattern="^\b[0-9]+\b$" ck = re.Replace(Str,"") If ck = "" Then reg = "不合法" Exit Function Else reg = "合法" End If Re.Pattern="^\b.*[\_|\-][0-9]*\b$" ck = re.Replace(Str,"") If ck = "" Then reg = "不合法" Exit Function Else reg = "合法" End If
End Functionarr = "-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" arr = Split(arr,",") For i = 0 To UBound(arr) Document.write arr(i) & "-" & reg(arr(i)) & "<br>" next </SCRIPT>
ls的不行啊
我在这个网址验证都不行呢。
http://tool.jz123.cn/regxtest.asp
还是不对啊,这个表达式怎么我输入‘test’就是不合法的呢
我用js的value.match(/^\b[^a-zA-Z0-9][a-zA-Z0-9_\-]+[^a-zA-Z]\b$/)来验证的
Function reg(Str)
Dim Re,Match,Matches
Set Re = New Regexp
Re.Global = True
Re.Ignorecase = True Re.Pattern="^\b[a-zA-Z0-9][a-zA-Z0-9_\-]+[a-zA-Z]\b$"
Str=re.Replace(Str,"") If str = "" Then
MsgBox "合法"
Else
MsgBox "不合法"
End if
End Function</SCRIPT><input type="text" id="sss" value=""><input type="button" value="ccc" onclick="reg(sss.value)">js我不是很熟悉,vbs的,测试过了,你放在页面中试试看吧
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]))$";
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
*/
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
*/
----------------------------------
test- 匹配吗?
true
----------------------------------
test-123 匹配吗?
true这两个你匹配错了
^(?![0-9]+$)[^-_]([a-zA-Z0-9]+|[_-](?![_0-9-]+$|$))+$
也是错误的,比如 :
#abc 匹配吗?
true
<SCRIPT Language="VBScript">
Function reg(Str)
Dim Re,Match,Matches
Set Re = New Regexp
Re.Global = True
Re.Ignorecase = True Re.Pattern="^\b[a-zA-Z0-9][a-zA-Z0-9_\-]+[a-zA-Z0-9]\b$"
ck = re.Replace(Str,"")
If ck = "" Then
reg = "合法"
Else
reg = "不合法"
Exit Function
End If Re.Pattern="^\b[0-9]+\b$"
ck = re.Replace(Str,"")
If ck = "" Then
reg = "不合法"
Exit Function
Else
reg = "合法"
End If Re.Pattern="^\b.*[\_|\-][0-9]*\b$"
ck = re.Replace(Str,"") If ck = "" Then
reg = "不合法"
Exit Function
Else
reg = "合法"
End If
End Functionarr = "-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"
arr = Split(arr,",")
For i = 0 To UBound(arr)
Document.write arr(i) & "-" & reg(arr(i)) & "<br>"
next
</SCRIPT>
btw,失误总是难免的,比如你的a123就不能通过
^(?![0-9]+$)(?![-_])([a-zA-Z0-9-]+|_(?![_0-9]+$|$))+$
这个错了
----------------------------------
test-123 匹配吗?
true
当初的是这个。不知道合并的时候 是不是给我改了
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]*$";//不存在-_时,至少有一个字母
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]$)";