<template>
<div>
<div v-html="string"></div>
</div>
</template>
<script>
export default {
data() {
return {
string: "",
};
},
mounted() {
this.string = this.getHref("<BR><BR>详情https://www.google.com.hk");
},
methods: {
// 识别网址并替换
getHref(content) {
if (!content) {
return "";
}
let urlPattern = /(https?:\/\/|www\.)[a-zA-Z_0-9\-@]+(\.\w[a-zA-Z_0-9\-:]+)+(\/[\(\)~#&\-=?\+\%/\.\w]+)?/g;
content = content.replace(urlPattern, function (match) {
var href = match;
if (match.indexOf("http") == -1) {
href = "http://" + match; //加粗样式
}
return '<a target="_blank" href="' + href + '">' + match + "</a>";
});
return content;
},
},
};
</script>
js查找字符串中的网址并替换为超链接,其余保持不变
最新推荐文章于 2024-02-06 10:06:51 发布