Question

This situation has no question yet. I wan't to detect if a string contains any link, that is all combinations of http,ftp,https, and the www., etc

Basically I want to prevent a string to contain any link.

I currently use:

name.split(/\s+/).find_all { |u| u =~ /^https?:/ }).count

What would be the best way to prevent any links in strings?

Was it helpful?

Solution

I ended up using:

errors.add(:name, "Your name cannot contain links") if name.match /\b(?:(?:mailto:\S+|(?:https?|ftp|file):\/\/)?(?:\w+\.)+[a-z]{2,6})\b/

Which works quite good in almost all situations. Hacked together from several sources online

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top