admin管理员组

文章数量:1279018

Hi i m having a text having multiple links wrapped inside text...

i want a regex(i m using javascript) which can parse the text and return a array of the links...

for example for the text...


testing
;feature=related

the regex would parse the text and return a array of the links

arr[0] = ""
arr[1] = ";feature=related"

i m trying to do so with the code...

var ytre =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig ;
var matches = new Array();

                    matches = ytre.exec(text);
                    var jm;
                    if (matches !=null )
                    {
                        for (jm=0; jm<matches.length; jm++)
                        {
                            console.log(matches[jm]);
                        }
                    }

but its not returning the appropriate results...

please help

thanks

Hi i m having a text having multiple links wrapped inside text...

i want a regex(i m using javascript) which can parse the text and return a array of the links...

for example for the text...

http://www.youtube./watch?v=-LiPMxFBLZY
testing
http://www.youtube./watch?v=Q3-l22b_Qg8&feature=related

the regex would parse the text and return a array of the links

arr[0] = "http://www.youtube./watch?v=-LiPMxFBLZY"
arr[1] = "http://www.youtube./watch?v=Q3-l22b_Qg8&feature=related"

i m trying to do so with the code...

var ytre =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig ;
var matches = new Array();

                    matches = ytre.exec(text);
                    var jm;
                    if (matches !=null )
                    {
                        for (jm=0; jm<matches.length; jm++)
                        {
                            console.log(matches[jm]);
                        }
                    }

but its not returning the appropriate results...

please help

thanks

Share Improve this question edited Dec 30, 2010 at 7:30 Tatu Ulmanen 125k34 gold badges189 silver badges185 bronze badges asked Dec 30, 2010 at 7:21 Pradyut BhattacharyaPradyut Bhattacharya 5,74813 gold badges58 silver badges86 bronze badges 1
  • library which does this: medialize.github./URI.js (see medialize.github./URI.js/docs.html#static-withinString) – chrisv Commented Jan 13, 2013 at 11:11
Add a ment  | 

2 Answers 2

Reset to default 8

How about:

var text = 'http://www.youtube./watch?v=-LiPMxFBLZY testing http://www.youtube./watch?v=Q3-l22b_Qg8&feature=related http://yahoo.';

var ytre = /(\b(https?|ftp|file):\/\/[\-A-Z0-9+&@#\/%?=~_|!:,.;]*[\-A-Z0-9+&@#\/%=~_|])/ig;

var resultArray = text.match(ytre);

See it

To parse URLs, using regexs, look at the RFC that defines URLs.

So to find regular expressions, use a variant that makes the protocol and authority non-optional, like /\b(([^:\/?#]+):)(\/\/([^\/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?/gi.

http://www.ietf/rfc/rfc3986.txt says

Appendix B. Parsing a URI Reference with a Regular Expression

As the "first-match-wins" algorithm is identical to the "greedy"
disambiguation method used by POSIX regular expressions, it is natural and monplace to use a regular expression for parsing the
potential five ponents of a URI reference.

The following line is the regular expression for breaking-down a
well-formed URI reference into its ponents.

  ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
   12            3  4          5       6  7        8 9

The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each
paired parenthesis). We refer to the value matched for subexpression as $. For example, matching the above expression to

  http://www.ics.uci.edu/pub/ietf/uri/#Related

results in the following subexpression matches:

  $1 = http:
  $2 = http
  $3 = //www.ics.uci.edu
  $4 = www.ics.uci.edu
  $5 = /pub/ietf/uri/
  $6 = <undefined>
  $7 = <undefined>
  $8 = #Related
  $9 = Related

where indicates that the ponent is not present, as is
the case for the query ponent in the above example. Therefore, we
can determine the value of the five ponents as

  scheme    = $2
  authority = $4
  path      = $5
  query     = $7
  fragment  = $9

本文标签: parse text with multiple links using regex in javascriptStack Overflow