
Regular Expression Question
Steve,
This is EXACTLY what I needed, thanks a lot. Here's what I came up with in
the end, it turns .com's into links:
var dot_com_match=/\b(http:\/\/)?(www.)?([-\w.]+.com)\b/gi;
s_str = s_str.replace(dot_com_match,
function ($0,$1,$2,$3)
{
return check_domain($0,$1,$2,$3)
}
);
function check_domain(full,http,www,domain)
{
if(!http&&!www)
return "<a href=\"http://"+domain+"\" target=\"_blank\">"+domain+"</a>";
else return full;
Quote:
}
Quote:
> The problem is that / is punctuation, so the \b matches the beginning of
> cnn.com whether it's preceded by http:// or not. That's the same problem
> that affects Tim's negative lookahead. First the regexp discards the
> http://, then it finds a word boundary (between the / and c) which isn't
> followed by http:// but is followed by cnn.com--a match!
> If you're looking for web site names embedded in other strings, you'll
need
> a two-step process--find the name and then determine whether or not it's
> preceded by http://.
> function testSite(s) {
> var re=/\b(http:\/\/)?[-\w.]+.com\b/
> if (re.test(s) && !RegExp.$1) return "Match"; else return "No Match";
> }
> WScript.echo(testSite("visit http://cnn.com"));
> WScript.echo(testSite("cnn.com is cool"));
> WScript.echo(testSite("fred isn't a web site"));
> Return true or false instead of "Match" or "No match" and you have a test.
> =-=-=
> Steve
> -=-=-
> > I'm not exactly sure how detailed your matching criteria is, but this
> should
> > work based on what you specified.
> > \b\w+\.com
> > : I'm trying to write a reg ex that matches web site names like this:
> > cnn.com
> > : but not http://cnn.com. I can't figure out how to have a reg ex
reject
> > the
> > : string if it contains 'http://'. This was my best attempt, evne
though
> I
> > am
> > : pretty sure the '?!' isn't right:
> > :
> > : \b(?!http:\/\/)[\w+\.\-]+(.com))
> > :
> > : I've tried a bunch of variations as well, but obviously none of them
> > worked.
> > :
> > : Tim