Help me to select urls with jquery

Discussion in 'jQuery' started by MD66, Jan 18, 2012.

  1. #1
    Solved! View solution.
    MD66, Jan 18, 2012 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    i think you can't select href values like regex does..
    what you can do is to select all <a> the test each of it inside .each();
     
    JohnnySchultz, Jan 19, 2012 IP
  3. MD66

    MD66 Well-Known Member

    Messages:
    137
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    103
    #3
    thank you for your reply
    i found this :
    http://forum.jquery.com/topic/regex-in-jquery


    but
    https?:\/\/(\w+).example.(\w+)

    doesn't work for me

    only select urls like
    http://www.mydomain.com
    and
    https://www.mydomain.com

    and doesn't select
    http://mydomain.com
    or
    http://www.subdomain.mydomain.com


    so i change it to
    /https?:\/\/(\w*)(\.*)(\w*)(\.*)example.(\w+)/;

    now it select all of types BUT
    if url be like this:
    http://subdomain[dot]subdomain[dot]subdomain.example.com
    it doesn't work
    is there any regex pattern that include all characters? (\w+) do not include [dot]

    my english is poor :rolleyes:

     
    MD66, Jan 19, 2012 IP
  4. #4
    Will this help?

    $.fn.replaceUrl = function() {
            var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
            this.each(function() {
                $(this).html(
                    $(this).html().replace(regexp,'<a href="$1">$1</a>‘)
                );
            });
            return $(this);
        }
    	//usage
    	$('p').replaceUrl();
    
    Code (markup):
    http://addyosmani.com/blog/50-jquery-snippets-for-developers/
     
    almondj, Jan 23, 2012 IP
  5. MD66

    MD66 Well-Known Member

    Messages:
    137
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    103
    #5
    thank you so much

    var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S*)(:[0-9]*)?example.(\w+)/;
    Code (markup):
     
    MD66, Jan 23, 2012 IP
  6. MD66

    MD66 Well-Known Member

    Messages:
    137
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    103
    #6
    Now i have new problem
    in this code

    
     <li>fdsafdsa <a href =http://www.example.com>salam1</a>fdsafdsafdsa </li>
     <a href =http://www.example.com>salam1</a>   
    
    Code (markup):
    i want to hide ONLY li elements that contains *a* tag
    when i use this code
    
    $("li:has(a)").css('display', function() {
         
          var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S*)(:[0-9]*)?(example.)(\w*)/;
          if(regexp.test(this.href))
                    return 'none';
          return 'inline';
       });
    
    Code (markup):
    it doesn't work
    because this.href refer to li tag i want to refer to a tag

    any solution?

    that's better to use hide function in jquery instead use display property to hide it
     
    MD66, Jan 24, 2012 IP