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