最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regex for allowing alphanumeric, special characters and not ending with @ or _ or - Stack Overflow

programmeradmin2浏览0评论

I am new to regex , I created below regex which allows alpha numeric and 3 special characters @._ but string should not end with @ or . or *

^[a-zA-Z0-9._@]*[^_][^.][^@]$

it validates abc@ but fails for abc.

I am new to regex , I created below regex which allows alpha numeric and 3 special characters @._ but string should not end with @ or . or *

^[a-zA-Z0-9._@]*[^_][^.][^@]$

it validates abc@ but fails for abc.

Share Improve this question edited Jul 26, 2021 at 12:11 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Jul 26, 2021 at 12:08 ahsaan khanahsaan khan 581 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Your pattern allows at least 3 characters, where the last 3 are negated character classes matching any char other than the listed.

The pattern ^[a-zA-Z0-9._@]*[^_][^.][^@]$ will match 3 newlines, and adding all the chars to a single character class ^[a-zA-Z0-9._@]*[^@._]$ will also match a single newline only.


If you want to allow all 3 "special" characters and match at least 3 characters in total you can repeat the character class 2 or more times using {2,} and match a single char at the end without the special characters.

^[a-zA-Z0-9._@]{2,}[a-zA-Z0-9]$

Regex demo

Matching as least a single char (and not end with . _ @)

^[a-zA-Z0-9._@]*[a-zA-Z0-9]$

Regex demo

if you include all the characters in the one character set, that'll work.

^[a-zA-Z0-9._@]*[^@._]$

Screenshot shows how different text examples would work (try it out on http://regexr.)

Leading ^ is the start of the paragraph
Trailing $ is the end of the paragraph
. is the everything {2,} means the more than 2 letters
[^@_] means one letter Not @ or _

^.{2,}[^@_]$

click here the answer

发布评论

评论列表(0)

  1. 暂无评论