有一行有链接和主题标签。您需要将这些链接包装在标签<a href=""></a>之类的标签中。但我不明白为什么它不能与链接正常工作,并且一个标签找不到没有空格的标签。这是我的例子:
var link = /(https?:\/\/|ftp:\/\/|www\.)((?![.,?!;:()]*(\s|$))[^\s]){2,}/gim
var hashTag = /(^|\s)(#[a-z\d-]+)/ig
function rebaseText(text) {
if (typeof text === 'string') {
if (text.match(link)) {
text = text.replace(link, "$1<a href={$2} class='link'>$2</a>");
}
if (text.match(hashTag)) {
text = text.replace(hashTag, "$1<a href='https://google.com/search?q=$2' class='hashtag'>$2</a>");
}
}
return text;
}
console.log(rebaseText('Hello world!!! https://google.com/ some text http://myweb.com/ lorem #hashTag1#hashTag2 qweqwewqeqweq #hashTag3'));
我是这样解决的,希望以后对某人有用: