开发者

Regex Match requiring @ and

I'm having trouble getting this regex to work. I need开发者_JS百科 a pattern that matches a single @ and a single .. Both are required.

I've tried /(@|\.)/ but this matches either/or. I need it to fail if one is missing.


You can try something like this:

\w+@\w+\.\w+

( assuming you are matching email like abc@xyz.com. You can make it more powerful and allow digits etc. If you are not concerned with email, then you are better off just looking for '@' and '.' in your string using string functions)


You want a String that looks like @.? Then you need /^(@\.|\.@)$/. Note: I am escaping the period because otherwise . stands for any single value.


If order is not important:

/^(?=[^.]*\.[^.]*$)(?=[^@]*@[^@]*$).*$/

matches a string that contains exactly one . and exactly one @.


The simplest I can think of is /@.*\.|\..*@/. Will match any string so long as it contains at least one @ and at least one . anywhere in the string.

Although probably a string function would be faster/better/more readable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜