SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex check hostname in a string

    Hi,
    I've to check if a string contains the hostname
    my 5 cents and it seems to work but not being at all
    a regex guru .....
    Code JavaScript:
    var re = new RegExp(window.location.hostname,'i');
    alert(re.test(str));
    str can be like:

    http://www.mydomain.com/
    http://www.mydomain.com
    www.mydomain.com/
    http://www.MYdomain.com/
    www.mydomain.com/action/id/12
    www.mydomain.com?id=12
    sub.mydomain.com


    and so on ....

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,117
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Hi,

    That does work.
    Code javascript:
    var l = window.location.hostname;
    var regex = new RegExp(l, 'i');
     
    var options = [
    	'http://www.mydomain.com/',
    	'http://www.mydomain.com',
    	'www.mydomain.com/',
    	'http://www.MYdomain.com/',
    	'www.mydomain.com/action/id/12',
    	'www.mydomain.com?id=12',
    	'sub.mydomain.com'
    ]
    for (var i=0, ii=options.length; i<ii; i++) {
    	alert(regex.test(options[i]));
    }

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everybody for the reply
    @markbrown4 I wanted be sure
    @Stomme poes
    var l = window.location.hostname;

  4. #4
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    I had no idea what you were talking about, so I had written a regex : )

    Off Topic:


    Man I'm already getting bogged on this tagging thing. Trying to find my mail just got harder.

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

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