Javascript - RegEx 大写和小写以及混合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12745930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - RegEx Uppercase and LowerCase and Mixed
提问by Genaut
I have this code:
我有这个代码:
var cadena = prompt("Cadena:");
document.write(mayusminus(cadena));
function mayusminus(cad){
var resultado = "Desconocido";
if(cad.match(new RegExp("[A-Z]"))){
resultado="mayúsculas";
}else{
if(cad.match(new RegExp("[a-z]"))){
resultado="minúsculas";
}else{
if(cad.match(new RegExp("[a-zA-z]"))){
resultado = "minúsculas y MAYUSCULAS";
}
}
}
return resultado;
}
I always have mayusculasor minusculas, never minusculas y MAYUSCULAS(MIXED), I am learning regexp and dont know my error yet :S
我一直有mayusculas或minusculas,从未minusculasÿMAYUSCULAS(MIXED),我正在学习正则表达式和不知道我的错误尚未:S
回答by Mike Samuel
new RegExp("[A-Z]")
matches when anycharacter in cadenais an upper-case letter.
To match when allcharacters are upper-case, use
当任何字符cadena是大写字母时匹配。要在所有字符都大写时进行匹配,请使用
new RegExp("^[A-Z]+$")
The ^forces it to start at the start, the $forces it to end at the end and the +ensures that between the end there are one or more of [A-Z].
在^它开始在开始力时,$它结束在端部的力和+保证了端部之间存在一个或多个[A-Z]。
回答by ?mega
I believe you wanted to use regex patterns ^[a-z]+$, ^[A-Z]+$and ^[a-zA-Z]+$.
我相信您想使用正则表达式模式^[a-z]+$,^[A-Z]+$并且^[a-zA-Z]+$.
In regex, the caret ^matches the position before the first character in the string. Similarly, $matches right after the last character in the string. Additionaly, +means one or more occurrences.
在正则表达式中,插入符号^匹配字符串中第一个字符之前的位置。同样,$在字符串中的最后一个字符之后匹配。此外,+表示一次或多次出现。
It is necessary to use ^and $in the pattern, if you want to ensure no other then listed characters are in the string.
如果要确保字符串中没有其他列出的字符,则必须在模式中使用^和$。
JavaScript:
JavaScript:
s = 'tEst';
r = (s.match(new RegExp("^[a-z]+$"))) ? 'minúsculas' :
(s.match(new RegExp("^[A-Z]+$"))) ? 'mayúsculas' :
(s.match(new RegExp("^[a-zA-Z]+$"))) ? 'minúsculas y mayúsculas' :
'desconocido';
Test this code here.
在此处测试此代码。
回答by sp00m
Let's say cad is foo:
假设 cad 是foo:
// will return false
if (cad.match(new RegExp("[A-Z]"))) {
resultado="mayúsculas";
// so will go there
} else {
// will return true
if (cad.match(new RegExp("[a-z]"))) {
// so will go there
resultado="minúsculas";
} else {
if (cad.match(new RegExp("[a-zA-z]"))) {
resultado = "minúsculas y MAYUSCULAS";
}
}
}
Now, let's say cad is FOO:
现在,让我们说 cad 是FOO:
// will return true
if (cad.match(new RegExp("[A-Z]"))) {
// so will go there
resultado="mayúsculas";
} else {
if (cad.match(new RegExp("[a-z]"))) {
resultado="minúsculas";
} else {
if (cad.match(new RegExp("[a-zA-z]"))) {
resultado = "minúsculas y MAYUSCULAS";
}
}
}
Finally, let's say cad is FoO:
最后,让我们说 cad 是FoO:
// will return true
if (cad.match(new RegExp("[A-Z]"))) {
// so will go there
resultado="mayúsculas";
} else {
if (cad.match(new RegExp("[a-z]"))) {
resultado="minúsculas";
} else {
if(cad.match(new RegExp("[a-zA-z]"))) {
resultado = "minúsculas y MAYUSCULAS";
}
}
}
As you can see, the nested elseis never visited.
如您所见,嵌套else从未被访问过。
What you can do is:
你可以做的是:
if (cad.match(new RegExp("^[A-Z]+$"))) {
resultado="mayúsculas";
} else if (cad.match(new RegExp("^[a-z]+$"))) {
resultado="minúsculas";
} else {
resultado = "minúsculas y MAYUSCULAS";
}
Explanation:
解释:
^means from the beginning of the string,
^表示从字符串的开头,
$means to the end of the string,
$表示到字符串的末尾,
<anything>+means at least one anything.
<anything>+意味着至少一个任何东西。
That said,
那说,
^[A-Z]+$means the string should only contains uppercased chars,
^[A-Z]+$意味着字符串应该只包含大写字符,
^[a-z]+$means the string should only contains lowercased chars.
^[a-z]+$意味着字符串应该只包含小写字符。
So if the string isn't only composed by uppercased or lowercased chars, the string contains both of them.
因此,如果字符串不仅由大写或小写字符组成,则该字符串将同时包含它们。

