匹配字符串中的链接并跳转
匹配出链接加a标签 用v-html绑定数据
<template>
<div v-html="formattedString"></div>
</template>
<script>
export default {
data() {
return {
originalString: '字符串字符串字符串字符串 https://www.baidu.com/'
}
},
computed: {
formattedString() {
const regex = /(https?:\/\/[^\s]+)/g;
return this.originalString.replace(regex, '<a href="$1">$1</a>');
}
}
}
</script>