views:

79

answers:

2

Hi..am just getting used to regex..but am unsure of how to use javascript variables within the regular expressions..

For example...as it stands..my code reads..

  if (window.location.hostname == 'localhost') {
    var $sURLDomain = 'http://localhost/';
}
else {
    var $sURLDomain = 'http://mysite.com';
}


$('#content a[href^=$sURLDomain]').addClass('internal');

Essentially i am setting the variable $sURLDomain dependant on whether im am on my local or live url.

I then want to test whether my links start with the $sURLDomain variable...and use Jquery to add a class to those specific links only....

This doesnt seem to be functioning, so am unsure of how to achieve this..

Any help would be appreciated...

+1  A: 

Try this:

$('#content a[href^="'+$sURLDomain+'"]').addClass('internal');

Or:

$('#content a').filter(function() {
    return this.href && this.href.substr(0, $sURLDomain.length) != $sURLDomain;
}).addClass('internal');
Gumbo
filter sounds extra work for the JS engine when rendering.
thephpdeveloper
@thephpdeveloper: It depends on the value of `$sURLDomain` if the `filter` method is required. But what do you mean by rendering? The JavaScript engine does not render anything within this code.
Gumbo
The first one has worked great for me..thanks...Wondering how you would identify links which dont start with the sURLDomain variable..thanks
namtax
@namtax: Use the `:not()` selector: `'#content a:not([href^="'+$sURLDomain+'"])'`.
Gumbo
works like a charm gumbo...many thanks
namtax
+1  A: 
$('#content a[href^=$sURLDomain]').addClass('internal');
  1. JavaScript doesn't do PHP-style string interpolation. You have to say '#content a[href^='+sURLDomain+']'.

  2. It's unusual to begin your JavaScript variables with a $. Some people use it as a kind of ‘Hungarian notation’-style flag that the variable is a wrapper object from jQuery or other library. It's arguable whether that's a good idea, but either way don't prefix your variables with $ just because that's what PHP does.

  3. The [^=...] selector checks the start of the attribute value for a literal string. Contrary to the question title, there is no regex here. The only connection to regular expressions is the use of ^ to mean ‘the start of the string’.

  4. If there's a character that's special for selectors in the URL, such as a ] or \, the lack of escaping means the selector expression will break. Probably unlikely in this particular case, but it may well be simpler (and easier to deploy given changing domain names) to say:

    for (var i= document.links; i-->0;)
        if (document.links[i].hostname===location.hostname)
            $(document.links[i]).addClass('internal');
    
bobince
Ha! `.hostname` FTW (http://msdn.microsoft.com/en-us/library/ms533785(VS.85,loband).aspx) You're loving these obscure properties on `<a>` elements aren't ya.
Crescent Fresh
Obscure?! That's a basic JavaScript feature going back to Netscape 2, yer whippersnapper! ;-)
bobince