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
2 Answers
Reset to default 8How 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 tohttp://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 asscheme = $2 authority = $4 path = $5 query = $7 fragment = $9
本文标签: parse text with multiple links using regex in javascriptStack Overflow
版权声明:本文标题:parse text with multiple links using regex in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741266424a2368555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论