RegExp Examples

Enter your First Name and your Age.

Please enter a URL.

<SCRIPT>

function getInfo(na) {
     var pattern = /(\w+)\s(\d+)/;  // "word" character(s) space digit(s)

     pattern.test(na);  // does na (name age) fit the pattern /(\w+)\s(\d+)/

     alert(RegExp.$1 + ", your age is " + RegExp.$2);  // RegExp.$1 ~ (\w+), RegExp.$2 ~ (\d+)
}

function getDomain(URL) {
     // (\w+) equals http
    
// ([\w.]+) equals espn.go.com which is between :\/\/ and \/ NOTE: we have to escape /
    
// (\S*) is everything after http://espn.go.com/
     var pattern = /(\w+):\/\/([\w.]+)\/(\S*)/;
     var result = URL.match(pattern);
    
// result[0] = "http://espn.go.com/nfl/index.html";
    
// result[1] = "http";
    
// result[2] = "espn.go.com";
    
// result[3] = "nfl/index.html";     

     var pattern2 = /.*\./;

     // look at espn.go.com and replace everything before the last . (dot) with ""
    
// we end up the domain, ie, com or edu or uk or de or etc...
     var domain = result[2].
replace(pattern2, "");

     // match is similar to split(...) except the the 0th element is the entire string
    
alert("URL = " + result[0]); 
     alert("Protocol = " + result[1]);  // ie, http or ftp or news or etc...
     alert("FQDN = " + result[2]);  // FQDN - Fully Qualified Domain Name
     alert("Virtual Path = /" + result[3]);
     alert("Domain Type = ." + domain);  // ie, com or edu or uk or de or etc...
}

</SCRIPT>