im-ui-mobile
Version:
A Vue3.0 + Typescript instant messaging component library for Uniapp
23 lines (20 loc) • 858 B
JavaScript
// 使用正则表达式匹配更广泛的URL格式(此正则由deepseek生成)
const regex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]|\bwww\.[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
const containUrl = (content) => {
return regex.test(content);
};
const replaceURLWithHTMLLinks = (content, color = '') => {
return content.replace(regex, (url) => {
// 如果URL不以http(s)://开头,则添加http://前缀
let fullUrl = url;
if (!url.startsWith("http")) {
fullUrl = "http://" + url;
}
const colorStyle = color ? `color: ${color};` : '';
return `<a href="${fullUrl}" target="_blank" style="${colorStyle}text-decoration: underline;">${url}</a>`;
});
};
export default {
containUrl,
replaceURLWithHTMLLinks
};