繁体   English   中英

排除正则表达式中的网址格式

[英]Exclude url pattern in regex

这是我的输入字符串

<div>http://google.com</div><span data-user-info="{\\"name\\":\\"subash\\", \\"url\\" : \\"http://userinfo.com?userid=33\\"}"></span><a href="https://contact.me"></a>http://byebye.com is a dummy website.

对于这种情况,我需要只匹配http的第一次和最后一次出现。 因为那些是html观点的innerText。 我们需要忽略属性值中的http。 我建立了以下正则表达式。

(?<!href=\"|src=\"|value=\"|href=\'|src=\'|value=\'|=)(http://|https://|ftp://|sftp://)

它适用于第一次和最后一次。 但这也匹配第二次出现的http。 我们不需要匹配的属性中的链接(http)。

仅供参考:我正在尝试消极前瞻,但这似乎没有帮助。 这是一个负向前瞻的人。

(?<!href=\"|src=\"|value=\"|href=\'|src=\'|value=\'|=)(http://|https://|ftp://|sftp://).*?(?!>)

有更多细节后更新

另一种方法是从正则表达式的“贪婪”中获益。 /(http).*(http)/g将匹配从“http”的第一次到最后一次出现的尽可能多的文本。 下面的示例说明了此行为。 (http)正在捕获组 - 用你的完整正则表达式替换它们。 我简化了正则表达式以便于理解。

var text ='<div>http://google.com</div><span data-user-info="{\"name\":\"subash\", \"url\" : \"http://userinfo.com?userid=33\"}"></span><a href="https://contact.me"></a>http://byebye.com is a dummy website.'
var regex = /(http).*(http)/g;
var match = regex.exec(text);
//match[0] is entire matched text
var firstMatch = match[1]; // = "http"
var lastMatch = match[2]; // = "http"

此示例特定于JavaScript,但Java regexps(以及许多其他正则表达式引擎)以相同的方式工作。 (http).*(http)也可以。


您的目标是匹配第一行和最后一行或第一次和最后一次出现的字符串吗?

如果前者是正确的,我会首先将文本拆分为行,然后将正则表达式匹配第一行和最后一行。

//Split into lines:
var lines = yourMultiLineText.split(/[\r\n]+/g);

如果后者是正确的,找到所有匹配你的基本模式,并从匹配数组中取第一个和最后一个,例如:

//Match using a simpler regex
var matches = yourMultiLineText.match(yourRegex);
//Store the result here
var result;
//Make sure that there are at least 2 matches in total for this to make sense.
if(matches.length > 1){
   //Grab the first and the last match.
   result = [matches[0], matches[matches.length - 1]];
} else {
   result = [];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM