【问题标题】:regex to grab url from string [duplicate]正则表达式从字符串中获取 url [重复]
【发布时间】:2015-10-30 18:37:08
【问题描述】:

我有这个 json 对象

arr[i].text 

返回

check this http://www.newlook.com/shop/womens/dresses/navy-short-sleeve-check-tunic-dress-_320165649

我只想返回带有正则表达式的 URL,如下所示:

 var urlreg = /(\bhttps?\:\/\/(www)?\.\w+\.\w+(\/[\w\d\-]+)*)/; 
match = urlreg.exec(arr[i].text );

但是不起作用,它是一个对象而不是字符串吗?

【问题讨论】:

  • 为我工作'check this http://www.newlook.com/shop/womens/dresses/navy-short-sleeve-check-tunic-dress-_320165649'.match(/(\bhttps?\:\/\/(www)?\.\w+\.\w+(\/[\w\d\-]+)*)/)[1]
  • 你能给我们看看 JSON 字符串吗?
  • 如果你像match = urlreg.exec(""+arr[i].text);那样做呢?请参阅jsfiddle.net/zdyksaft,您的正则表达式正在使用简单的字符串输入。
  • 也适合我。但是,这个正则表达式可以更普遍地用于域/子域匹配。检查我的答案。

标签: javascript regex


【解决方案1】:

试试:var urlreg = /(https?:\/\/(\w+\.)+\w+(\/[\w\-_]+)+)/\/?

这是demo

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 2020-02-15
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多