将段落中的 Web 链接分离到 JS 数组中
Separate web links from paragraph into a JS array
我检查了上面的所有答案,看看我的答案是否得到了回答,请不要将其标记为重复
在我正在构建的应用程序中,有一个div
<div class="paragraphlink"> </div>
我允许用户输入任何这样的文本
莱科宁是 http://www.formula1.com 进入《福布斯》杂志名人100强榜单的两位一级方程式车手之一,另一位是费尔南多·阿隆索·http://www.google.com。他在《福布斯》杂志的 http://www.forbesmagazine.com 中排名第36位
如果您注意到上述段落中存在网址,您能否帮助我使用JavaScript代码从上述段落中提取所有URL?
你可以正则表达式对象
var reg = new RegExp(/http(s)?:'/'/[a-z0-9_-'.'/]+/ig);
var html = "Räikkönen was among the two Formula One drivers http://www.formula1.com who made it into the Forbes magazine's The Celebrity 100 list, the other being Fernando Alonso http://www.google.com. He is 36th on Forbes magazine's http://www.forbesmagazine.com"
html.match(reg);
将返回一个 URL 数组。
您可以在文本上使用匹配函数来提取网址,例如:
var str = "Räikkönen was among the two Formula One drivers http://www.formula1.com who made it into the Forbes magazine's The Celebrity 100 list, the other being Fernando Alonso http://www.google.com. He is 36th on Forbes magazine's http://www.forbesmagazine.com";
var urls = str.match(/http(s)?:'/'/[a-z0-9_-'.'/]+/ig);
// now the var "urls" is in array with your URL
但是正则表达式在这里很简单,您可以在许多网站上找到更好的正则表达式。
使用正则表达式
像这样的正则表达式
/(http|https|ftp|ftps)':'/'/[a-zA-Z0-9'-'.]+'.[a-zA-Z]{2,3}('/'S*)?/g
将匹配网址
有关Javascript正则表达式的更多信息,请检查此和此
相关文章:
- JS数组-检查对象值是否重复
- 将JS数组附加到变量并显示它
- 正在尝试将JSON文件放入JS数组
- 删除JS数组中的最小数字
- 调用laravel{{action(Controller@method}}通过传递vue.js数组中的变量
- 如何将JS数组添加到Meteor自动表单条目中
- 了解JS数组和拼接
- JS数组称为“;name”;行为怪异
- 在js数组中不断循环并暂停
- 根据范围过滤角度.js数组
- 循环访问 JS 数组 + 数组属性
- 为什么当我在JS数组上调用.toString时得到“对象对象”
- Ember.js数组控制器计算属性的复选框
- 我的JS数组打印只显示结束值
- 如何在AngularJs中将json数组转换为js数组
- JS数组无理由地传递值更改
- 是否可以引用JS数组/对象中的另一个元素
- 将jQuery事件函数应用于新的Knockout.js数组元素
- 通过多列和多个值过滤angular js数组
- 在js数组中查找下一个和上一个键