开发者

Help Regarding the result of this regular expression

 var pat = /(^[\w\.\+\-=]+@[\w\.\-]+\.[\w\-]+$)/g

I wan开发者_高级运维t to use this as a email validator.. I found it in a forum.

But I am not aware of ^ /g and forward slash and the structure /( * )/g ?

.+-= what does this represent ??


Your pattern

/(^[\w\.\+\-=]+@[\w\.\-]+\.[\w\-]+$)/g

contains those elements:

/ ... / Regex pattern delimiter

/ ... /g Regex pattern modifier here g global pattern ==> match all occurrences

^ Anchor for the start of the string, forces the pattern to match from the start of the string on.

$ Anchor for the end of the string

[] Square brackets define a character class, i.e. this construct matches one character of those included in this class. [\w\.\+\-=] matches either a word character or a . + - =
Inside such a character class you don't need to escape most of the characters. So [\w.+\-=] would have the same meaning (and [\w.+=-] also)

\w is a word character, depends on your regex engine, but at least a-zA-Z0-9 and _

+ means matches the previous part at least once, [\w\.\+\-=]+ matches for example "Foobar", "++++=", ".", ".Foo=098+-"

There exist several online test tools for regexes. See your regex for example here on Regexr


^ is the anchor for the start of string.

The / are the regex delimiters.

The g is the global pattern modifier.


The "g" stands for "global", which tells Perl to replace all matches, and not just the first one. Options are typically indicated including the slash, like "/g", even though you do not add an extra slash, and even though you could use any non-word character instead of slashes.

Example:

s/cat/dog/g 

< The zoo had wild dogs, bobcats, lions, and other wild cats.

All cat will be replaced by dog

The zoo had wild dogs, bobdogs, lions, and other wild dogs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜