views:

46

answers:

4
+1  Q: 

javascript regex

var siteurl = window.location;
var patt= /(http:\/\/)(www.)*([a-zA-Z0-9\.\-]*)(.*)/i;

Now i want to get value of $3, how to?

+1  A: 
var three = siteurl.replace(patt, "$3");

You can also call "exec" on the regex, and that will return an array. The first element (index 0) of the array will be all the text that matched the complete pattern, and then subsequent indexes will contain the values of the capturing groups in the regex.

var groups = patt.exec(siteurl);
var three = groups[3];
Pointy
+1  A: 
var three = patt.exec(siteurl)[3];

The exec method returns an "array" corresponding to the matching elements of the first match. [0] is the entire matched substring, [1] is the text matched by the first capturing group, ... Subsequent calls to exec would return subsequent matches if your regex had the "g" (global) flag set (it does not, and it does not need to).

Note that if you are not sure whether or not the match exists, you should add some error handling:

var result = patt.exec(siteurl);
if(result && result[3]) {
    var three = result[3];
}
else {
    ...
}
idealmachine
+1  A: 
RegExp.$3

You may want to consider not using window.location, though. Using one of the following would make your RegExp simpler and avoid the need to test for different protocols (eg https, ftp, etc):

window.location.host
window.location.hostname

Examples


  • option 1 - window.location

    var siteurl = window.location;
    var patt= /(http:\/\/)(www.)*([a-zA-Z0-9\.\-]*)(.*)/i;
    
    
    siteurl.match(patt);
    alert(RegExp.$3);
    
  • option 2 - window.location.hostname

    var siteurl = window.location.hostname;
    var patt= /(www.)*([a-zA-Z0-9\.\-]*)/i;
    
    
    siteurl.match(patt);
    alert(RegExp.$2);
    

Edits


As noted, this method is deprecated, but not yet removed in JS 1.5. It is still available for you to use.

The only thing you need to be aware of is modifying the .lastIndex property if you will be using multiple times so that the object doesn't wrap.

vol7ron
uhh .... you sure about that?
Pointy
According to MDC, that's deprecated: https://developer.mozilla.org/en/JavaScript/Reference/Deprecated_Features#RegExp_Methods
idealmachine
**@Pointy:** yes it still works. **@idealmachine:** yes you are also correct. -- I've included a note as I think that is worth mentioning. I don't think this was worth a `-1`; deprecated != unavailable and as the MDC says, it is still used in `replace()`. You can test it yourself if you think it won't work.
vol7ron
Well before you added the "Example" part, it wasn't exactly clear what you meant. I didn't downvote however.
Pointy
Tried to get the answer up there fast and the explanation later :)
vol7ron
A: 

I would recommend not writing a regex for this.

If you want to get the hostname, just do what vol7ron suggests.

If you really do need to parse a URI in JavaScript, take a look at Appendix B of thehttp://www.apps.ietf.org/rfc/rfc3986.html and use the match array returned from myString.match(myRegex).

Appendix B. Parsing a URI Reference with a Regular Expression

As the "first-match-wins" algorithm is identical to the "greedy" disambiguation method used by POSIX regular expressions, it is natural and commonplace to use a regular expression for parsing the potential five components of a URI reference.

The following line is the regular expression for breaking-down a well-formed URI reference into its components.

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

 12            3  4          5       6  7        8 9

The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each paired parenthesis). We refer to the value matched for subexpression

as $. For example, matching the above expression to http://www.ics.uci.edu/pub/ietf/uri/#Related

results in the following subexpression matches:

  $1 = http:
  $2 = http
  $3 = //www.ics.uci.edu
  $4 = www.ics.uci.edu
  $5 = /pub/ietf/uri/
  $6 = <undefined>
  $7 = <undefined>
  $8 = #Related
  $9 = Related where <undefined> indicates that the component is not

present, as is the case for the query component in the above example. Therefore, we can determine the value of the five components as

  scheme    = $2
  authority = $4
  path      = $5
  query     = $7
  fragment  = $9
Mike Samuel