我有一些字符需要如上所述替换,但我不知道如何替换:
要替换的字符:
first | end | <day> | | <red> | </red>| <a ###> | </> |
day => 获取当前日期(例如:14)
红色 => 颜色红色
链接> => 链接>
输入:你好
输出:你好Siro先生
我的聊天记录。
你能告诉我如何编写一个通用函数来检查上述标签的替换吗? 这是我的代码:
export const formatTags = (content) => {
const firstTag = "<red>";
const secondTag = "</red>";
const tagsIndex = [...content.matchAll(new RegExp(firstTag, "gi"))].map(
(a) => a.index
);
const initialContent = content;
tagsIndex.forEach((index) => {
const tagContent = initialContent.substring(
index + firstTag.length,
initialContent.indexOf(secondTag, index)
);
if (firstTag === "<red>") {
content = content.replaceAll(
`${firstTag}${tagContent}${secondTag}`,
`<span style="color: red">${tagContent || "わからない"}</span>`
);
}
});
return content;
};
<span
:class="(msg.image || msg.file) && msg.text ? 'mt-2' : ''"
v-html="msg.text"
v-linkified:options="{
className: currentUserId === msg.senderId
? 'message-external-link'
: '',
}"
/>
抱歉我的英语不太好!
谢谢大家!
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号