首页 > 解决方案 > 在新选项卡中打开本地链接

问题描述

我有一个场景,我在超链接中链接文本中的所有 URL。但是当用户键入 www. 时,浏览器会将其作为本地 URL 并将链接绑定到应用程序 URL。

我想知道如何在新选项卡中打开本地 URL。

var replaceTextWithURL = systemNotificationText.replace(/(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi, function(text, link) {
  console.log(text, link);
  return '<a href="' + link + '" class="systemNotificationExtLink" target="_blank"> ' + link + ' </a>';
  //var httpUrl = link.startsWith("http");
  //var httpsUrl = link.startsWith("https");

  //if (httpUrl || httpsUrl) {
  //    return '<a href="' + text + '" target="_blank" class="systemNotificationExtLink"> ' + text + ' </a>';
  //}
  //else {
  //    return '<a href="http://' + text + '" target="_blank" class="systemNotificationExtLink"> ' + text + ' </a>';
  //}
});
var newLineText = replaceTextWithURL.replace(/\r?\n/g, '<br />');
$("#systemNotificationPreview").html(newLineText);

标签: javascriptjqueryhtml

解决方案


你能完善你的问题吗,你要问的不是很清楚?


推荐阅读