如何编写javascript正则表达式以将此格式的超链接[*](*)替换为html超链接?
我需要以下格式的带有链接的解析文本:如何编写javascript正则表达式以将此格式的超链接[*](*)替换为html超链接?,javascript,regex,Javascript,Regex,我需要以下格式的带有链接的解析文本: [html title](http://www.htmlpage.com) http://www.htmlpage.com http://i.imgur.com/OgQ9Uaf.jpg 这两个字符串的输出为: <a href='http://www.htmlpage.com'>html title</a> <a href='http://www.htmlpage.com'>http://www.htmlpage.com&
[html title](http://www.htmlpage.com)
http://www.htmlpage.com
http://i.imgur.com/OgQ9Uaf.jpg
这两个字符串的输出为:
<a href='http://www.htmlpage.com'>html title</a>
<a href='http://www.htmlpage.com'>http://www.htmlpage.com</a>
<a href='http://i.imgur.com/OgQ9Uaf.jpg'>http://i.imgur.com/OgQ9Uaf.jpg</a>
输出:
<a href='http://www.htmlpage.com'>html title</a><a href='http://www.htmlpage.com'>html title</a>
<a href='http://www.htmlpage.com'>html title</a> <a href='http://www.htmlpage.com'>html title</a>
<a href='http://www.htmlpage.com'>html title</a> wejwelfj <a href='http://www.htmlpage.com'>http://www.htmlpage.com</a>
为简洁起见,我将发布我尝试过的正则表达式,而不是整个find/replace函数:
var matchArray2 = inString.match(/\[.*\]\(.*\)/g);
对于匹配的[*](*)
,不起作用,因为匹配了[]()[]()
真的,我想就是这样。一旦匹配了,我就搜索匹配的()和[]来解析链接和链接文本并构建href标记。我从临时字符串中删除匹配项,以便在第二次查找纯超链接时不匹配它们:
var plainLinkArray = tempString2.match(/http\S*:\/\/\S*/g);
我没有用正则表达式解析任何html。我正在解析一个字符串并试图输出html
编辑:我添加了一个要求,要求它在事实发生后解析第三个链接
我的最终解决方案(基于@Cerbrus的回答):
函数parseAndHandleHyperlinks(inString)
{
var result=inString.replace(/\[(.+?)\]\((https?:\/\/.+?)\)/g';
返回结果。替换(/(?:|^)(https?\:\/\/\/[a-zA-Z0-9/(]+)/g');
}
str.replace(/\[(.*?)\]\(.*?)/gi,”);
这假设字符串中没有错误的括号,URL中也没有括号
然后:
str.replace(/(\s| ^)(https?:\/\/.*?(=\s|$)/gi,“$1”)
这与一个类似于“http”的URL相匹配,该URL的前面不会立即加上一个“(该URL将由上一个替换项添加)。当然,如果您有更好的表达式,请随意使用
编辑:我编辑了答案,因为我没有意识到JS没有lookbehind语法。相反,您可以看到表达式匹配任何空格或行首,以匹配普通的http
链接。捕获的空格必须放回(因此$1
)。在结尾处进行前瞻,以确保捕获到下一个空格(或表达式结尾)之前的所有内容。如果空格对您来说不是一个好的边界,您必须找到一个更好的边界。请尝试以下正则表达式:
/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g
var s = "[html title](http://www.htmlpage.com)[html title](http://www.htmlpage.com)\n\
[html title](http://www.htmlpage.com) [html title](http://www.htmlpage.com)\n\
[html title](http://www.htmlpage.com) wejwelfj http://www.htmlpage.com";
string.replace(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g, '<a href="$2">$1</a>');
现在捕获()/[]
中的两个字符串,并将其放置在以下字符串中:
'<a href="$2">$1</a>';
”;
这适用于“有问题”字符串:
var s=“[This](http://i.imgur.com/iIlhrEu.jpg)一个先让我哭了,然后一旦闸门打开[这个](http://i.imgur.com/IwSNFVD.jpg)一个又做了一次[这个](http://i.imgur.com/hxIwPKJ.jpg).啊,感觉。我得去拥抱某人/什么东西。”
s、 替换(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/(]+?)\))/g',)
//结果:
“一个先让我哭了,然后一旦闸门打开,一个又哭了。啊,感觉。我得去拥抱某人/某物。”
更多输入“不正确”的示例:
var s=“[Th][[is](http://x.com)\n\
[此](http://x(.com)\n\
[this](http://x.com)
s、 替换(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/(]+?)\))/g',)
// "
//
//(www.com)
你不能真的责怪最后一行破坏了,因为没有办法知道用户是否打算在那里停止url
要捕获松散的URL,请添加以下内容:
.replace(/(?: |^)(https?\:\/\/[a-zA-Z0-9/.(]+)/g, ' <a href="$1">$1</a>');
。替换(/(?:| ^)(https?\:\/\/\/[a-zA-Z0-9/(]+)/g');
(?:|^)
位捕获一个字符串开头
或空格
字符,因此它还将匹配以url开头的行。似乎您正在尝试将标记语法转换为HTML。标记语法还没有一个规范(我指的是语法,不是行为规范)因此,您将蒙着眼睛四处走动,并尝试在改造轮子的同时为您不想要的行为加入错误修复。我建议您使用现有的实现,而不是自己编写实现。例如,StackOve中当前使用的是Markdown的JS实现rflow
如果您仍然想要一个正则表达式解决方案,下面是我的尝试。请注意,我不知道随着您的进步,它是否能很好地与降价的其他功能配合使用(如果您确实这样做的话)
上面的正则表达式应该为[description](url)
链接风格(不支持标题)捕获部分Pagedown行为(我不相信它捕获了所有内容,Pagedown的源代码太复杂,无法一次读取)。上面的正则表达式是由Pagedown源代码中使用的两个不同正则表达式混合而成的
一些特点:
- 捕获组1包含
[]
中的文本,捕获组2包含URL
- 通过使用
\
例如[a\[1\]],允许在文本部分[]
内转义[
和]
(http://link.com)
。不过,您需要做一些额外的处理
- 允许1级
()
内部链接,在以下情况下非常有用:[String.valueOf](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf(双重))
- 在链接之后和
之前留出空间)
我没有考虑这个正则表达式中的裸链接
参考:
?这里的许多ppl都会告诉你,用正则表达式解析HTML…这样疯狂就存在了,当然,如果你必须处理的唯一标记是可能的,但一定要查看其他标记我不可能想到一个有用的地方…@jahroy:你看到这里的url是如何生成的吗?让我给你一个提示:[title](url)
或[标题][1][1]:url
。像这样的解析器在论坛和其他类似的社区网站上很有用。此外,@EliasVanOotegem:试图解释HTML文档和试图将一种特定格式解析为HTML之间是有区别的。@Cerburs:你说得对,我只是说regex、HTML和parse,所以我得出了错误的结论。当我发表评论时,有但是,没有代码显示OP尝试了什么thusfar,所以我留下了注释isYour fir
str.replace(/\[(.*?)\]\((.*?)\)/gi, '<a href="$2">$1</a>');
str.replace(/(\s|^)(https?:\/\/.*?)(?=\s|$)/gi, '$1<a href="$2">$2</a>')
/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g
var s = "[html title](http://www.htmlpage.com)[html title](http://www.htmlpage.com)\n\
[html title](http://www.htmlpage.com) [html title](http://www.htmlpage.com)\n\
[html title](http://www.htmlpage.com) wejwelfj http://www.htmlpage.com";
string.replace(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g, '<a href="$2">$1</a>');
# / - Regex Start
# \[ - a `[` character (escaped)
# (.+?) - Followed by any amount of words, grouped, non-greedy, so it won't match past:
# \] - a `]` character (escaped)
# \( - Followed by a `(` character (escaped)
# (https?:\/\/
# [a-zA-Z0-9/.(]+?) - Followed by a string that starts with `http://` or `https://`
# \) - Followed by a `)` character (escaped)
# /g - End of the regex, search globally.
'<a href="$2">$1</a>';
var s = "[This](http://i.imgur.com/iIlhrEu.jpg) one got me crying first, then once the floodgates were opened [this](http://i.imgur.com/IwSNFVD.jpg) one did it again and [this](http://i.imgur.com/hxIwPKJ.jpg). Ugh, feels. Gotta go hug someone/something."
s.replace(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g, '<a href="$2">$1</a>')
// Result:
'<a href="http://i.imgur.com/iIlhrEu.jpg">This</a> one got me crying first, then once the floodgates were opened <a href="http://i.imgur.com/IwSNFVD.jpg">this</a> one did it again and <a href="http://i.imgur.com/hxIwPKJ.jpg">this</a>. Ugh, feels. Gotta go hug someone/something.'
var s = "[Th][][is](http://x.com)\n\
[this](http://x(.com)\n\
[this](http://x).com)"
s.replace(/\[(.+?)\]\((https?:\/\/[a-zA-Z0-9/.(]+?)\)/g, '<a href="$2">$1</a>')
// "<a href="http://x.com">Th][][is</a>
// <a href="http://x(.com">this</a>
// <a href="http://x">this</a>.com)"
.replace(/(?: |^)(https?\:\/\/[a-zA-Z0-9/.(]+)/g, ' <a href="$1">$1</a>');
/\[((?:[^\[\]\\]|\\.)+)\]\((https?:\/\/(?:[-A-Z0-9+&@#\/%=~_|\[\]](?= *\))|[-A-Z0-9+&@#\/%?=~_|\[\]!:,.;](?! *\))|\([-A-Z0-9+&@#\/%?=~_|\[\]!:,.;(]*\))+) *\)/i