Javascript String parseURL()
String.prototype.parseURL = function() { return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=@]+/g, function(url) { return url.link(url); });//ww w. j a va2 s .c o m }; String.prototype.parseUsername = function() { return this.replace(/(?:^|\s)[@]+[A-Za-z0-9-_]+/g, function(u) { var username = u.replace("@","") if (username.charAt(0) === ' ') { username = username.replace(/\s+/g, ''); } return u.link("http://twitter.com/"+username); }); }; var parseLinks = function(string) { return(string.parseURL().parseUsername()); } module.exports = parseLinks;
String.prototype.parseURL = function() { return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) { var link="<a target='_blank' href='"+url+"'>"+url+"</a>" return link;/* w w w. j av a 2 s .c o m*/ }); };
/*//from w w w . j a v a 2 s.com Write a script that parses an URL address given in the format: [protocol]://[server]/[resource] and extracts from it the [protocol], [server] and [resource] elements. Return the elements in a JSON object.*/ String.prototype.parseURL = function () { var string = String(this), url = { protocol: undefined, server: undefined, resource: undefined }, regExUrlParse = /(\w{3,10}):\/\/([A-z.]+)\/(.+)/g, parsedArr = regExUrlParse.exec(string); url.protocol = parsedArr[1]; url.server = parsedArr[2]; url.resource = parsedArr[3]; return url; }; var urlAsStr = 'http:\/\/telerikacademy.com\/Courses\/Courses\/Details\/239'; console.log(urlAsStr.parseURL());