javascript – How Can I Use Regex to Match All the Urls in This String?

Going off of what you currently have, you can just append |[a-zA-Z0-9]+\.[^\s]{2,} to the end of your expression. The resulting line will look like this:

var expression = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})|[a-zA-Z0-9]+\.[^\s]{2,}/gi;

This could be cleaner, but it’ll do what you’re asking.

Edit:

If you’re okay with something slightly more permissive that can pull the same URLs out, you can try this expression:

var expression = /(?:https?:\/\/)?(?:www\.)?[\w.-]+\.\S{2,}/gi;

Read more here: Source link