-1

Simple question. Given a text area which might have a few urls copied and pasted, I would like to parse all of them out in to an array. Don't want to replace the contents, but get all the urls in the text area in to an array using Javascript.

EDIT

http://jsfiddle.net/sujesharukil/hyEem/19/

Created a fiddle. I have it mostly working. The only issue is, it does not pick up query string parameters. May be that should have been the question.

this is what I put in the text area

www.google.com ssdfsdfasdfasdf http://someplace.com?x=sdfsdf

regex = new RegExp(/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);

returns two items, but no query string params for the second one.

Help will be appreciated.

Sujesh Arukil
  • 2,469
  • 16
  • 37
  • 2
    What format are the URLs going to be in? Starting with http, www, something different? What is some example text that you can show the desired results on so we can help? – Walls Mar 21 '13 at 18:30
  • @epascarello. I am not asking you to do my job. I asked a question. If you think you need more information, then please ask so (minus the sarcasm accompanied by it). I have added a fiddle and updated my question. Hopefully you can show your magic now and then say TADA!! – Sujesh Arukil Mar 21 '13 at 18:54
  • possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Lance Roberts Mar 21 '13 at 21:52

2 Answers2

2

Assuming they'll be delimited by newlines, you could do this:

var array = document.getElementById('yourTextAreasID').value.split('\n');

Or if URLs aren't the only thing in the textarea, you'll have to use a regex. Something like this:

var array = document.getElementById('yourTextAreasID').value.split(/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}([\/-a-zA-Z0-9@:%_\+.~#\?&//=]*)*/gi);

It's a good regex for finding URLs in many formats.

Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
2

RegEx for finding urls in a textarea: ((?:https?:|www\.)[^\s]+)
Explained demo here: http://regex101.com/r/vZ0aE9

CSᵠ
  • 10,049
  • 9
  • 41
  • 64