Recommand · October 14, 2021 0

How to delect third part LINKS in textarea?

I am Trying to give notice to users, If they enter third party URLS in textarea,
I have found solution, But that is not exactly what i want,

if(/(http|https|ftp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i.test(content)){
    console.log('Link Detected');
}else if (/^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$/i.test(content)) {
    console.log('Link Detected');
}

This Solution, works, But the problem is, I want to allow external Image links and any page links from my site.

1) https://google.com => Not Allowed
2) https://google.com/image.jpg => Allowed
3) https: // mysite .com/contact => Allowed

Any suggestions?

I don’t know about your complete requirement, but i assume that you want to allow subpages and images of external urls. Since each image is ‘implicit’ always a subpage of the given url, you can simplify your regex that it must not end with an ‘/’ or any toplevel domain.

I am not an regex expert, but to implement that you can remove any trailing ‘/’ from your url first and then check if the last 3 or 4 characters equals ‘.xxx’ or ‘.xx’ (x is a wildcard here).