문제

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