web: optimize tagExp expression

pull/351/head
Michael Li 2 years ago
parent 79a5299864
commit 576986f502
No known key found for this signature in database

@ -1,19 +1,30 @@
export const parsePostTag = (content: string) => { export const parsePostTag = (content: string) => {
const tags: string[] = [] const tags: string[] = [];
const users: string[] = [] const users: string[] = [];
var tagExp = /(#|)([^#@])+?\s+?/g // 这⾥中⽂#和英⽂#都会识别 var tagExp = /(#|)([^#@\s])+?\s+?/g; // 这⾥中⽂#和英⽂#都会识别
var atExp = /@([a-zA-Z0-9])+?\s+?/g // 这⾥中⽂#和英⽂#都会识别 var atExp = /@([a-zA-Z0-9])+?\s+?/g; // 这⾥中⽂#和英⽂#都会识别
content = content content = content
.replace(/<[^>]*?>/gi, '') .replace(/<[^>]*?>/gi, "")
.replace(/(.*?)<\/[^>]*?>/gi, '') .replace(/(.*?)<\/[^>]*?>/gi, "")
.replace(tagExp, item => { .replace(tagExp, (item) => {
tags.push(item.substr(1).trim()) tags.push(item.substr(1).trim());
return '<a class="hash-link" data-detail="tag:' + encodeURIComponent(item.substr(1).trim()) + '">' + item.trim() + '</a> ' return (
}) '<a class="hash-link" data-detail="tag:' +
.replace(atExp, item => { encodeURIComponent(item.substr(1).trim()) +
users.push(item.substr(1).trim()) '">' +
return '<a class="hash-link" data-detail="user:' + encodeURIComponent(item.substr(1).trim()) + '">' + item.trim() + '</a> ' item.trim() +
}) "</a> "
return { content, tags, users } );
} })
.replace(atExp, (item) => {
users.push(item.substr(1).trim());
return (
'<a class="hash-link" data-detail="user:' +
encodeURIComponent(item.substr(1).trim()) +
'">' +
item.trim() +
"</a> "
);
});
return { content, tags, users };
};

Loading…
Cancel
Save