我有一个字符串,基本上是HTML模板:
<p>lkmlsdkmlskdmflsdk <span class="mention-user" data-initials="AG" data-name="AndréèêÃÃÃÃ_Güntheräö" id="cxid5024056"><span contenteditable="false"><!----><span class="initials"><!---->AG<!----></span>AndréèêÃÃÃÃ_Güntheräö<!----><!----></span></span> laksmlaksm lkmalskamlskmaldksml</p>
我使用这个regex函数删除了提及的HTML,并更改了提及的id,因此我应该返回这个字符串:
<p>lkmlsdkmlskdmflsdk {mentionuser=cxid5024056} laksmlaksm kmalskamlskmaldksml</p>
结果我得到了这个:
<p>asñald,ñsdl, {mentionuser=cxid5024056}" data-initials="AG" data-name="AndréèêÃÃÃÃ_Güntheräö laksmlaksm kmalskamlskmaldksml</p>
为此,我将使用此函数替换提提id格式的提提HTML:
public replaceMention(text: string): string {
if (!text) {
return '';
}
const contentMention = text.split(/(cxid[0-9]{1,8})/);
for (let index = 0; index < contentMention.length; index++) {
if (contentMention[index].match(/<span[^>]*id="/)) {
contentMention[index] = contentMention[index].replace(/<span[^>]*id="/, '');
}
if (contentMention[index].match(/">[^>]*<span[^>]*><[^>]*>(<img[^>]*>)?(<span[^>]*><[^>]*>[^>]*<[^>]*><\/span>)?([^>]*)?(([a-zA-Z\-_.äöüéèêÃÃÃà etc]+)?\s?[a-zA-Z\-_.äöüéèêÃÃÃà etc]+?_[a-zA-Z\-_.äöüéèêÃÃÃà etc]+)([^>]*)?(<[^>]*><[^>]*>)?<\/span>[^>]*<\/span>( )?/)) {
contentMention[index] = contentMention[index].replace(
/">[^>]*<span[^>]*><[^>]*>(<img[^>]*>)?(<span[^>]*><[^>]*>[^>]*<[^>]*><\/span>)?([^>]*)?(([a-zA-Z\-_.äöüéèêÃÃÃà etc]+)?\s?[a-zA-Z\-_.äöüéèêÃÃÃà etc]+?_[a-zA-Z\-_.äöüéèêÃÃÃà etc]+)([^>]*)?(<[^>]*><[^>]*>)?<\/span>[^>]*<\/span>( )?/g,
''
);
}
if (contentMention[index].match(/^(cxid[0-9]{1,8})$/)) {
if (contentMention[index - 1].includes('{mentionuser=') || contentMention[index - 1] === undefined) {
//
} else {
contentMention[index] = `{mentionuser=${contentMention[index]}}`;
}
}
}
let content = contentMention.join('').trim();
console.log('====', content);
return content;
}
在Chrome中,Firefox可以正常工作,但是在Edge和internetexplorer中就不行了。知道原因是什么吗?