0

I have this json object

arr[i].text 

which returns

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

I want to return only the URL with a regex like so:

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

but doesn't work, is it something to with it being an object and not a string?

user1937021
  • 10,151
  • 22
  • 81
  • 143
  • 1
    Works for me `'check this http://www.newlook.com/shop/womens/dresses/navy-short-sleeve-check-tunic-dress-_320165649'.match(/(\bhttps?\:\/\/(www)?\.\w+\.\w+(\/[\w\d\-]+)*)/)[1]` – Tushar Aug 07 '15 at 10:36
  • Can you show us the JSON string? – Ismael Miguel Aug 07 '15 at 10:39
  • What if you do it like `match = urlreg.exec(""+arr[i].text);`? See http://jsfiddle.net/zdyksaft/, your regex is working with a simple string input. – Wiktor Stribiżew Aug 07 '15 at 10:42
  • Works for me too. However, this regex can be more generalised for domain/subdomain matching. Check my answer. – Haywire Aug 07 '15 at 10:56

1 Answers1

0

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

Here is a demo

Haywire
  • 858
  • 3
  • 14
  • 30