vue实现把字符串中的所有@内容,替换成带标签的

前言:

        目前有个需求是,要把输入框里面的@还有姓名高亮。

要求:

1、必须用 v-html ,带标签的给他渲染

2、把字符串中的@全部查找出来,替换掉,注意要过滤已经替换好的,不然就是无限循环了

实现方法:

// 消息展示中处理@样式
const textPointTo = (content:any) => {
  let index = value.indexOf('@');
  while (index !== -1) {
    const endIndex = value.indexOf(' ', index);
    const replaceStr = value.substring(index, endIndex !== -1 ? endIndex : undefined);
    value = value.replace(replaceStr, `<span class='replyPointTo'>${replaceStr.split('@')[1]}</span>&nbsp;`);
    index = value.indexOf('@', index + 1);
 }
 return value.replace(/PointTo'>/g, "PointTo'>@")
};

猜你喜欢

转载自blog.csdn.net/weixin_44727080/article/details/132489250