Forum Moderators: open

Message Too Old, No Replies

More fun with regex, plug in a default if $n doesn't exist

         

csdude55

9:08 pm on Dec 6, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm specifically playing with this to convert www.foo.com, http://www.foo.com, or https://www.foo.com to <a href='https://www.foo.com'>www.foo.com</a>.

a = a.replace(
/(^|>|\s)(https?:\/\/)?(www\.\S+)/gi,
'$1<a href="https://$3">$3</a>');

The question that comes up is when the user submits http://www.foo.com, this will convert it to HTTPS. Which is normally fine, but every once in a blue moon the destination doesn't have HTTPS set up.

I could just convert them all to HTTP, of course, and let the destination figure it out. But before I do that, is there a way to make this replace with $2 if it exists, but if $2 doesn't exist then default to http://?

lucy24

1:20 am on Dec 7, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



/(^|>|\s)
Do you need to constrain it to those specific characters? Otherwise you could simply say \b and sidestep the capture. Instead you can capture the (https?)--which is already right there, only it can be $1 instead of $2--and reuse it.

Edit: Whoops, overlooked the possiblity of (https?:\/\/) not existing at all. If you can afford the extra lines, there's always "if $2 == ''" and so on.

csdude55

4:48 am on Dec 7, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you need to constrain it to those specific characters?

I knew you were gonna ask that! LOL

I don't remember the details now, but awhile back we addressed that very thing. Over a year ago, probably. I feel like \b was matching characters that we didn't want it to match, but there've been a loooot of whiskey between then and now!

If you can afford the extra lines, there's always "if $2 == ''" and so on.

Yeah, that's the path I was on, too...

a = a.replace(
/(^|>|\s)(https?:\/\/)?(www\.\S+)/gi,
($match, $1, $2, $3) => {
return $1 + '<a href="' + ($2 || 'http://') + $3 + '">' + $3 + '</a>';
}
);

I thought there might be a better way, though. This works, but I just know that when I look at it in a year I'll have no idea what I was thinking :-/

Fotiman

3:17 pm on Dec 7, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could use a default value in the function parameters:


a = a.replace(
/(^|>|\s)(https?:\/\/)?(www\.\S+)/gi,
($match, $1, $2 = 'http://', $3) => {
return $1 + '<a href="' + $2 + $3 + '">' + $3 + '</a>';
}
);

csdude55

7:20 pm on Dec 7, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Didn't know I could do that! That's much easier to read, thanks :-)