开发者

RegEx for matching "A-Z, a-z, 0-9, _" and "."

I 开发者_开发技巧need a regex which will allow only A-Z, a-z, 0-9, the _ character, and dot (.) in the input.

I tried:

[A-Za-z0-9_.] 

But, it did not work. How can I fix it?


^[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.

See for example: http://msdn.microsoft.com/en-us/library/h5181w5w(v=vs.110).aspx


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?


You could simply use ^[\w.]+ to match A-Z, a-z, 0-9 and _


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

use warnings;
use strict;

my $str = '!!!.###';
$str =~ s/[A-Za-z_.]/X/g;
print "$str\n";

produces

!!!X###


regex: /^[a-zA-Z0-9_.]$/i

This works


For me, i was looking for a regex like Gmail has, should allow a-z, A-z, 0-9, _, . only here is the Yup format for it i used:

Email: Yup.string()
      .email("Please Provide Valid Email")
      .matches(
        /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(.\w{2,3})+$/,
        "Only a-z A-Z _ . 0-9 are allowed in this Field"
       )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜