diff --git a/lib/shared/markdown.js b/lib/shared/markdown.js --- a/lib/shared/markdown.js +++ b/lib/shared/markdown.js @@ -347,6 +347,8 @@ ); } +const ensRegex: RegExp = /^[a-zA-Z0-9-]{3,}\.eth$/; + export { paragraphRegex, paragraphStripTrailingNewlineRegex, @@ -372,4 +374,5 @@ stripSpoilersFromNotifications, stripSpoilersFromMarkdownAST, parseChatMention, + ensRegex, }; diff --git a/lib/shared/markdown.test.js b/lib/shared/markdown.test.js --- a/lib/shared/markdown.test.js +++ b/lib/shared/markdown.test.js @@ -1,6 +1,6 @@ // @flow -import { spoilerRegex } from './markdown.js'; +import { spoilerRegex, ensRegex } from './markdown.js'; import { chatMentionRegex } from './mention-utils.js'; // Each of the below tests is subject to the following RegEx pattern: @@ -141,4 +141,40 @@ expect('@[256|1:test]]').not.toMatch(chatMentionRegex); expect('@[[256|1:test]').not.toMatch(chatMentionRegex); }); + + describe('ensRegex', () => { + it('should match all valid ENS names', () => { + expect('foo.eth').toMatch(ensRegex); + expect('jack.eth').toMatch(ensRegex); + expect('thisuserhasareallylongname.eth').toMatch(ensRegex); + }); + + it('should not match any usernames names less than 3 characters', () => { + expect('fo.eth').not.toMatch(ensRegex); + expect('a.eth').not.toMatch(ensRegex); + expect('.eth').not.toMatch(ensRegex); + }); + + it('should not match any usernames with spaces', () => { + expect('foo bar.eth').not.toMatch(ensRegex); + expect('user1.eth user2.eth').not.toMatch(ensRegex); + expect('user1.eth and user2.eth').not.toMatch(ensRegex); + }); + + it('should not match any usernames with underscores', () => { + expect('foo_bar.eth').not.toMatch(ensRegex); + expect('_user1.eth').not.toMatch(ensRegex); + }); + + it('should not match any usernames without .eth', () => { + expect('foo').not.toMatch(ensRegex); + expect('ryan.eth2').not.toMatch(ensRegex); + }); + + it('should not match any usernames with special characters', () => { + expect('foo.eth!').not.toMatch(ensRegex); + expect('foo.eth#').not.toMatch(ensRegex); + expect('foo$.eth').not.toMatch(ensRegex); + }); + }); });