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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top