www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Recognizing URL's in textarea

  1. #1
    Join Date
    May 2009
    Posts
    5

    resolved [RESOLVED] Recognizing URL's in textarea

    Hello. I would like to know how I can find multiple URL's in a textarea then use "for each" to run code that I want to run on each URL.

    For example, if the user typed this into the textarea "Hello, please visit http://example.com and http://google.com" then I would want to be able to find each URL in the textarea and run the code that I want to run on each URL.

    Sorry that I'm a bit vague.

    Regards,
    Chris.

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    It'll take a regexp. Matching URLs is quite difficult, because the http:// is optional, as is the www in most cases. This seems a pretty good one.
    Great wit and madness are near allied, and fine a line their bounds divide.

  3. #3
    Join Date
    May 2009
    Posts
    5
    Thanks for that, I know http:// is optional but if it was compulsory in my website then is there a way I can use "for each" for everything that begins with http:// without spaces?

  4. #4
    Join Date
    May 2009
    Posts
    5
    How can I run code on every item in an array? For example:

    Code:
    function isUrl(s) {
    	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    	return regexp.test(s);
    }
    
    function findURLs(){
    var array = document.getElementById("textarea").split(" ");
    // for each item in array, run the below code
    if(isUrl(array[ITEM IN ARRAY]){
    document.getElementById("textarea").replace(array[ITEM IN ARRAY], "URL REPLACEMENT HERE");
    }
    }
    How can I run the isURL() function on each item in the array and how do I show the current item (e.g. array[CURRENT ITEM])?

    Please help, and thanks Declan1991 for the regexp.
    Last edited by Chris252; 05-11-2009 at 06:28 AM. Reason: Fixed an incorrect piece of code

  5. #5
    Join Date
    May 2009
    Posts
    5
    I have not got this working. For those who stumble across this thread in the future, here is my code.

    Code:
    function isUrl(s) {
    	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    	return regexp.test(s);
    }
    
    function findURLs(){
    var array = document.getElementById("textarea").split(" ");
    for (var n = 0; n < array.length; n++){
    if(isUrl(array[n]){
    document.getElementById("textarea").replace(array[n], "URL REPLACEMENT HERE");
    }
    }
    }
    Last edited by Chris252; 05-11-2009 at 06:44 AM. Reason: Added [CODE] tags

  6. #6
    Join Date
    Nov 2012
    Posts
    1

    Updated: jQuery Version

    I was able to get this to work with some slight modifications.

    Code:
    function findURLs(){
    	var Msg = jQuery('#Message');
    	var MsgStr = Msg.val();
    	var Arr = MsgStr.split(" ");
    	for (var n = 0; n < Arr.length; n++){
    		txtStr = Arr[n]; 
    		if(isUrl(txtStr)){
    			MsgStr = MsgStr.replace(txtStr, "URL REPLACEMENT HERE");
    		} 
    	}
    	Msg.val(MsgStr);
    }

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    A variant is possible without jQuery and without loop with something like this (only a demo)

    Code:
    function findURLs(){
        var txt = document.getElementById("textarea");
    
        // Your reg exp with a g at the end
        var rgx= = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g;
    
        // A replace which call an anonymous function for each URL
        newTxt=txt.replace(rgx,function(){var i,c='';for (i=0;i<arguments.length;i++) c=+'\n'+arguments[i];
            // The first argument is the matched pattern and the other the sub-patterns defined by brakets 
            alert(c);
            //...
            // The return value replace the matched pattern  
            return "URL REPLACEMENT HERE";
        });
    }
    Last edited by 007Julien; 11-27-2012 at 06:58 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles