How can i get a array of url's inside a string like :

我怎样才能在字符串中得到一个url数组:

"http://www.google.de, http://www.lycos.de, http://www.facebook.de"

Already tried this. This returns just the first coma. It should be reserved and global.

已经尝试过了。这只返回第一个昏迷。它应该是保留的和全球性的。

new RegExp(", *", "g").exec(attrValue);

1 个解决方案

#1


3

Use String.split() instead:

请改用String.split():

var origin = "http://www.google.de, http://www.lycos.de, http://www.facebook.de";
var urls   = origin.split(/,\s*/);

You don't have to use new RegExp construct, as JS has quite a convenient notation for regex literals.

您不必使用新的RegExp构造,因为JS对正则表达式文字有相当方便的表示法。


For the sake of completeness, here's how it can be done with RegExp.match:

为了完整起见,这里是如何使用RegExp.match完成的:

var origin = "http://www.google.de, http://www.lycos.de, http://www.facebook.de";
var urls   = origin.match(/.+?(?=,\s*|$)/g);

The idea is essentially the same: for each pattern match iteration (as /g global modifier is used), we stop (using look-ahead subexpression) when either a sequence of comma and optional whitespace has been found, or when we reach the end of the string.

这个想法基本相同:对于每个模式匹配迭代(使用/ g全局修饰符),当找到逗号和可选空格序列时,或者当我们到达结尾时,我们停止(使用前瞻子表达式)的字符串。

更多相关文章

  1. JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
  2. java 如何获取动态网页内容,返回字符串
  3. NodeJS - 解析JSON(只有字符串或数字)
  4. 如何在JavaScript中对字符串排序
  5. JSON.parse(xhr.responseText)意外的字符串错误
  6. 如何在url中将特殊字符作为查询字符串传递
  7. JS计算任意字符串宽度
  8. 如何在JavaScript中按大写和小写分割字符串?
  9. 使用jackson json将属性添加到json字符串

随机推荐

  1. golang gin怎么安装
  2. golang中的defer关键字什么时候生效
  3. golang leaf用的多吗
  4. golang gf怎么使用
  5. golang map有什么用
  6. golang和erlang区别
  7. golang gin框架错误处理
  8. golang中创建错误的方法
  9. golang chan是否关闭
  10. golang web开发乱码的原因与解决方法