From 576986f5024ddc8b6e655cffec83f4381e0f1310 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Tue, 11 Apr 2023 09:56:09 +0800 Subject: [PATCH] web: optimize tagExp expression --- web/src/utils/content.ts | 47 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/web/src/utils/content.ts b/web/src/utils/content.ts index c1138af1..f48e55be 100644 --- a/web/src/utils/content.ts +++ b/web/src/utils/content.ts @@ -1,19 +1,30 @@ - export const parsePostTag = (content: string) => { - const tags: string[] = [] - const users: string[] = [] - var tagExp = /(#|#)([^#@])+?\s+?/g // 这⾥中⽂#和英⽂#都会识别 - var atExp = /@([a-zA-Z0-9])+?\s+?/g // 这⾥中⽂#和英⽂#都会识别 - content = content - .replace(/<[^>]*?>/gi, '') - .replace(/(.*?)<\/[^>]*?>/gi, '') - .replace(tagExp, item => { - tags.push(item.substr(1).trim()) - return '' + item.trim() + ' ' - }) - .replace(atExp, item => { - users.push(item.substr(1).trim()) - return '' + item.trim() + ' ' - }) - return { content, tags, users } -} \ No newline at end of file + const tags: string[] = []; + const users: string[] = []; + var tagExp = /(#|#)([^#@\s])+?\s+?/g; // 这⾥中⽂#和英⽂#都会识别 + var atExp = /@([a-zA-Z0-9])+?\s+?/g; // 这⾥中⽂#和英⽂#都会识别 + content = content + .replace(/<[^>]*?>/gi, "") + .replace(/(.*?)<\/[^>]*?>/gi, "") + .replace(tagExp, (item) => { + tags.push(item.substr(1).trim()); + return ( + '' + + item.trim() + + " " + ); + }) + .replace(atExp, (item) => { + users.push(item.substr(1).trim()); + return ( + '' + + item.trim() + + " " + ); + }); + return { content, tags, users }; +};