如何编写一个javascript正则表达式来替换此格式[*](*)的超链接与html超链接?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何编写一个javascript正则表达式来替换此格式[*](*)的超链接与html超链接?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4233字,纯文字阅读大概需要7分钟。
内容图文
![如何编写一个javascript正则表达式来替换此格式[*](*)的超链接与html超链接?](/upload/InfoBanner/zyjiaocheng/704/763662ffb12a4674b8d88cf09d9637c2.jpg)
我需要使用以下格式的链接的解析文本:
[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>
字符串可以包含任意数量的这些链接,即:
[html title](http://www.htmlpage.com)[html title](http://www.htmlpage.com)
[html title](http://www.htmlpage.com) [html title](http://www.htmlpage.com)
[html title](http://www.htmlpage.com) wejwelfj http://www.htmlpage.com
输出:
<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>
我有一个非常长的函数,通过传递字符串3次做一个正常的工作,但我无法成功解析此字符串:
[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.
为简洁起见,我将发布我尝试过的正则表达式而不是整个查找/替换函数:
var matchArray2 = inString.match(/\[.*\]\(.*\)/g);
匹配[*](*),因为[]()[]()匹配不起作用
我猜,真的就是这样.一旦我进行了匹配,我搜索匹配的()和[]来解析链接文本并构建href标记.我从临时字符串中删除匹配项,因此当我第二次访问以查找纯超链接时,我不匹配它们:
var plainLinkArray = tempString2.match(/http\S*:\/\/\S*/g);
我没有用正则表达式解析任何html.我正在解析一个字符串并尝试输出html.
编辑:我添加了在事后解析第三个链接http://i.imgur.com/OgQ9Uaf.jpg的要求.
我的最终解决方案(根据@ Cerbrus的回答):
function parseAndHandleHyperlinks(inString)
{
var result = inString.replace(/\[(.+?)\]\((https?:\/\/.+?)\)/g, '<a href="$2">$1</a>');
return result.replace(/(?: |^)(https?\:\/\/[a-zA-Z0-9/.(]+)/g, ' <a href="$1">$1</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.
现在捕获()/ []中的2个字符串,并将其放在以下字符串中:
'<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)"
你不能真的责怪破坏的最后一行,因为没有办法知道用户是否打算在那里停止网址.
要捕获松散的URL,请添加以下内容:
.replace(/(?: |^)(https?\:\/\/[a-zA-Z0-9/.(]+)/g, ' <a href="$1">$1</a>');
(?:| ^)位捕获字符串开头或空格字符,因此它也匹配以url开头的行.
内容总结
以上是互联网集市为您收集整理的如何编写一个javascript正则表达式来替换此格式[*](*)的超链接与html超链接?全部内容,希望文章能够帮你解决如何编写一个javascript正则表达式来替换此格式[*](*)的超链接与html超链接?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。