Javascript RegEx,查找URL';s,其中不以=”开头;

Javascript RegEx,查找URL';s,其中不以=”开头;,javascript,jquery,regex,Javascript,Jquery,Regex,我正在尝试构建一个函数来查找字符串中的URL,并将它们更改为链接。但是我不想找到已经在HTML标记中的URL(如),您可以尝试以下方法: /(((^[^\="|\>])([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w

我正在尝试构建一个函数来查找字符串中的URL,并将它们更改为链接。但是我不想找到已经在HTML标记中的URL(如

),您可以尝试以下方法:

/(((^[^\="|\>])([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;
string.replace(
/(]*>[^>]*)|您的_REGEX_此处/g,
功能(匹配、链接、您的捕获组等){
如果(链接){
返回链接
}
返回所需的\u更换件
}
)
以上匹配已经有效的
标记或您所使用的URL查找字符串 正在寻找,以先到者为准。捕获组用于检测 两个链接中哪一个匹配。如果匹配了有效链接,只需返回它即可
未修改。否则返回所需的替换项。

您可以尝试以下方法:

/(((^[^\="|\>])([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;
string.replace(
/(]*>[^>]*)|您的_REGEX_此处/g,
功能(匹配、链接、您的捕获组等){
如果(链接){
返回链接
}
返回所需的\u更换件
}
)
以上匹配已经有效的
标记或您所使用的URL查找字符串 正在寻找,以先到者为准。捕获组用于检测 两个链接中哪一个匹配。如果匹配了有效链接,只需返回它即可
未修改。否则返回所需的替换项。

另一个方法有点难看。我通过所有匹配项进行迭代,为不匹配项重新生成源html,对于匹配项,我检查matchIndex-1处的字符,并添加链接标记或不添加链接标记

这样做的好处是已经非常复杂的regexp不会变得更复杂,您可以使用完整的javascript检查当前字符串是否是html元素的一部分

如果您将迭代代码分解,它甚至可能最终看起来很好

(^[^\="|\>])
var-urlRegEx=/(([A-Za-z]{3,9}:(?:\/\/)(?:[\-;:&=\+\$,\w]+@);[A-Za-z0-9\.\-]+\124;(?:www.\.[\-;:&=\+\$,\w]+.[A-Za-z0-9\-]+(?:\/\++++\+++--%%\\\\\\\\/\\\\.\w].*(?:[\++++++\\\\\\+-/.-%.-%.[\\\\\-/.-%.-%.-/-%%.[\\\\\\\.-/.-%%.-/.--%%.[\.---%%.[\\-%%.-/.-;
var source=$('#source').html();
var dest=“”;
var lastMatchEnd=0;
while((match=urlRegEx.exec(source))!=null){
dest+=source.substring(lastmachend,match.index);
var end=match.index+match[0]。长度;
var lastChar=source.charAt(match.index-1);
如果(lastChar='''”| | lastChar=='='>'){//内部链接
dest+=匹配[0];
}否则{
dest+=“”;
}
lastMatchEnd=结束;
}
dest+=source.substring(lastMatchEnd);
$('#target').html(dest);

另一种方法有点难看。我通过所有匹配项进行迭代,为不匹配项重新生成源html,对于匹配项,我检查matchIndex-1处的字符,并添加链接标记或不添加链接标记

这样做的好处是已经非常复杂的regexp不会变得更复杂,您可以使用完整的javascript检查当前字符串是否是html元素的一部分

如果您将迭代代码分解,它甚至可能最终看起来很好

(^[^\="|\>])
var-urlRegEx=/(([A-Za-z]{3,9}:(?:\/\/)(?:[\-;:&=\+\$,\w]+@);[A-Za-z0-9\.\-]+\124;(?:www.\.[\-;:&=\+\$,\w]+.[A-Za-z0-9\-]+(?:\/\++++\+++--%%\\\\\\\\/\\\\.\w].*(?:[\++++++\\\\\\+-/.-%.-%.[\\\\\-/.-%.-%.-/-%%.[\\\\\\\.-/.-%%.-/.--%%.[\.---%%.[\\-%%.-/.-;
var source=$('#source').html();
var dest=“”;
var lastMatchEnd=0;
while((match=urlRegEx.exec(source))!=null){
dest+=source.substring(lastmachend,match.index);
var end=match.index+match[0]。长度;
var lastChar=source.charAt(match.index-1);
如果(lastChar='''”| | lastChar=='='>'){//内部链接
dest+=匹配[0];
}否则{
dest+=“”;
}
lastMatchEnd=结束;
}
dest+=source.substring(lastMatchEnd);
$('#target').html(dest);
/(((^[^\="|\>])([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;
(^[^\="|\>])
string.replace(
  /(<a[^>]*>[^>]*<\a>)|YOUR_REGEX_HERE/g,
  function(match, link, YOUR_CAPTURE_GROUP_1, etc) {
    if (link) {
        return link
    }
    return YOUR_DESIRED_REPLACEMENT
  }
)
var urlRegEx = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;

var source = $('#source').html();
var dest = "";
var lastMatchEnd = 0;
while ((match = urlRegEx.exec(source)) != null) {
  dest += source.substring(lastMatchEnd, match.index);
  var end = match.index + match[0].length;
  var lastChar = source.charAt(match.index - 1);
  if(lastChar == '"' || lastChar == '>') { // inside link
    dest += match[0];
  } else {
    dest += "<a href=''>" + match[0] + "</a>";
  }
  lastMatchEnd = end;
}
dest += source.substring(lastMatchEnd);
$('#target').html(dest);