Regexp exec列表的url由昏迷“,*”分隔
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全局修饰符),当找到逗号和可选空格序列时,或者当我们到达结尾时,我们停止(使用前瞻子表达式)的字符串。
更多相关文章
- JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
- java 如何获取动态网页内容,返回字符串
- NodeJS - 解析JSON(只有字符串或数字)
- 如何在JavaScript中对字符串排序
- JSON.parse(xhr.responseText)意外的字符串错误
- 如何在url中将特殊字符作为查询字符串传递
- JS计算任意字符串宽度
- 如何在JavaScript中按大写和小写分割字符串?
- 使用jackson json将属性添加到json字符串