I need a regex which will allow only A-Z, a-z, 0-9, the _ character, and dot (.) in the input. I tried:
我需要一个regex,它只允许输入的a-z、a-z、0-9、_字符和点(.)。我试着:
[A-Za-z0-9_.]
But it did not work. How can I fix it?
但它没有起作用。我怎样才能修好它?
3 个解决方案
#1
40
^[A-Za-z0-9_.]+$
From beginning until the end of the string, match one or more of these characters.
从开始到字符串结束,匹配一个或多个这些字符。
Edit:
编辑:
Note that ^
and $
match the beginning and the end of a line. When multiline is enabled, this can mean that one line matches, but not the complete string.
注意,$匹配开始和结束的一行。当启用多行时,这可能意味着一行匹配,而不是完整的字符串。
Use \A
for the beginning of the string, and \z
for the end.
使用\A开头的字符串,然后\z结束。
See for example: http://msdn.microsoft.com/en-us/library/h5181w5w(v=vs.110).aspx
例如:http://msdn.microsoft.com/en-us/library/h5181w5w(v = vs.110). aspx
#2
5
Working from what you've given I'll assume you want to check that someone has NOT entered any letters other than the ones you've listed. For that to work you want to search for any characters other than those listed:
根据你所给出的,我假设你想要检查某人没有输入你列出的其他字母。为了让你工作,你需要搜索除列出的以外的任何字符:
[^A-Za-z0-9_.]
And use that in a match in your code, something like:
在你的代码中使用它,比如:
if ( /[^A-Za-z0-9_.]/.match( your_input_string ) ) {
alert( "you have entered invalid data" );
}
Hows that?
豪视安科公司吗?
#3
0
Maybe, you need to specify more exactly what didn't work and in what environment you are.
也许,您需要更确切地指定哪些没有工作,哪些环境是您的。
As to the claim that the dot is special in a charackter class, this is not true in every programming environment. For example the following perl script
至于说点在charackter类中是特殊的,在每个编程环境中都不是这样。例如下面的perl脚本。
use warnings;
use strict;
my $str = '!!!.###';
$str =~ s/[A-Za-z_.]/X/g;
print "$str\n";
produces
生产
!!!X###
#1
40
^[A-Za-z0-9_.]+$
From beginning until the end of the string, match one or more of these characters.
从开始到字符串结束,匹配一个或多个这些字符。
Edit:
编辑:
Note that ^
and $
match the beginning and the end of a line. When multiline is enabled, this can mean that one line matches, but not the complete string.
注意,$匹配开始和结束的一行。当启用多行时,这可能意味着一行匹配,而不是完整的字符串。
Use \A
for the beginning of the string, and \z
for the end.
使用\A开头的字符串,然后\z结束。
See for example: http://msdn.microsoft.com/en-us/library/h5181w5w(v=vs.110).aspx
例如:http://msdn.microsoft.com/en-us/library/h5181w5w(v = vs.110). aspx
#2
5
Working from what you've given I'll assume you want to check that someone has NOT entered any letters other than the ones you've listed. For that to work you want to search for any characters other than those listed:
根据你所给出的,我假设你想要检查某人没有输入你列出的其他字母。为了让你工作,你需要搜索除列出的以外的任何字符:
[^A-Za-z0-9_.]
And use that in a match in your code, something like:
在你的代码中使用它,比如:
if ( /[^A-Za-z0-9_.]/.match( your_input_string ) ) {
alert( "you have entered invalid data" );
}
Hows that?
豪视安科公司吗?
#3
0
Maybe, you need to specify more exactly what didn't work and in what environment you are.
也许,您需要更确切地指定哪些没有工作,哪些环境是您的。
As to the claim that the dot is special in a charackter class, this is not true in every programming environment. For example the following perl script
至于说点在charackter类中是特殊的,在每个编程环境中都不是这样。例如下面的perl脚本。
use warnings;
use strict;
my $str = '!!!.###';
$str =~ s/[A-Za-z_.]/X/g;
print "$str\n";
produces
生产
!!!X###