2013-10-30 24 views

回答

1

你可以用正則表達式在文本中找到所有電子郵件地址,如/\S*\@\S*/Test it on Rubular,可能不完美),然後用你選擇的任何東西替換所有匹配。

email_regex = /\S*\@\S*/ 
text = "This is [email protected] test string. Regex is [email protected] amazing." 

result = text.gsub(email_regex, 'email_has_been_replaced') 

p result 
# => "This is email_has_been_replaced test string. Regex is email_has_been_replaced amazing." 

在ActiveRecord模型:

class Post < AR::B 
    EMAIL_REGEX = /\S*\@\S*/ 
    before_validation :remove_email_addresses_from_body 

    private 

    def remove_email_addresses_from_body 
    self.body = body.gsub(EMAIL_REGEX, 'hidden_email') 
    end 
end 
+0

您是如何把在一個模型? –

+0

你可以例如替換'before_save'或'before_validation'回調中的電子郵件地址。 –

+0

你的偉人!像magik一樣工作! –

相關問題