Created
August 12, 2011 09:49
-
-
Save pasberth/1141788 to your computer and use it in GitHub Desktop.
URL を置換するだけです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class String | |
AVAILABLE_CHARS_IN_URI = [ | |
'\;', '\/', '\?', '\:', '\@', '\&', '\=', '\+', '\$', '\,', | |
'\-', '\_', '\.', '\!', '\~', '\*', '\\\'', '\(', '\)', | |
'\%', '0-1', 'a-z', 'A-Z' '\#' | |
] | |
REGEXP_OF_URL = /https?\:\/\/[#{AVAILABLE_CHARS_IN_URI.join('')}]+/ | |
def replace_urls &blk | |
gsub(REGEXP_OF_URL, &blk) | |
end | |
end | |
require "test/unit" | |
class TestReplaceURLs < Test::Unit::TestCase | |
def setup | |
@text = "abcd efg http://example.com hijk lmn https://example.com/index.html opqr stu vwxyz" | |
@replaced = "abcd efg [[http://example.com]] hijk lmn [[https://example.com/index.html]] opqr stu vwxyz" | |
end | |
def test_replace_urls | |
assert_equal(@replaced, @text.replace_urls { |url| "[[#{url}]]" }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment