6
6
# UrlRegex
7
7
8
8
Provides the best known regex for validating and extracting URLs.
9
- It builds on amazing work done by [ Diego Perini] ( https://gist.github.com/dperini/729294 )
9
+ It builds on amazing work done by [ Diego Perini] ( https://gist.github.com/dperini/729294 )
10
10
and [ Mathias Bynens] ( https://mathiasbynens.be/demo/url-regex ) .
11
11
12
- Why do we need a gem for this regex?
12
+ Why do we need a gem for this regex?
13
13
14
14
- You don't need to follow changes and improvements of original regex.
15
15
- You can slightly customize the regex: a scheme can be optional, and you can get the regex for validation or parsing.
@@ -33,12 +33,12 @@ Or install it yourself as:
33
33
Get the regex:
34
34
35
35
UrlRegex.get(options)
36
-
36
+
37
37
where options are:
38
38
39
39
- ` scheme_required ` indicates that schema is required, defaults to ` true ` .
40
40
41
- - ` mode ` can gets either ` :validation ` or ` :parsing ` , defaults to ` :validation ` .
41
+ - ` mode ` can gets either ` :validation ` , ` :parsing ` or ` :javascript ` , defaults to ` :validation ` .
42
42
43
43
` :validation ` asks to return the regex for validation, namely, with ` \A ` prefix, and with ` \z ` postfix.
44
44
That means, it matches whole text:
@@ -47,17 +47,27 @@ That means, it matches whole text:
47
47
# => false
48
48
UrlRegex.get(mode: :validation).match('link: https://www.google.com').nil?
49
49
# => true
50
-
50
+
51
51
` :parsing ` asks to return the regex for parsing:
52
52
53
53
str = 'links: google.com https://google.com?t=1'
54
54
str.scan(UrlRegex.get(mode: :parsing))
55
55
# => ["https://google.com?t=1"]
56
-
56
+
57
57
# schema is not required
58
58
str.scan(UrlRegex.get(scheme_required: false, mode: :parsing))
59
59
# => ["google.com", "https://google.com?t=1"]
60
60
61
+ ` :javascript ` asks to return the regex formatted for use in Javascript files or as ` pattern ` attribute values on HTML inputs. For this purpose, you'd use the ` source ` method on the Regexp object instance in order to produce a string that Javascript will understand. These examples make use of the Rails ` text_field ` method to generate HTML input elements.
62
+
63
+ regex = UrlRegex.get(mode: :javascript)
64
+ text_field(:site, :url, pattern: regex.source)
65
+ # => <input type="text" id="site_url" name="site[url]" pattern="[javascript URL regex]" />
66
+
67
+ regex = UrlRegex.get(scheme_required: false, mode: :javascript)
68
+ text_field(:site, :url, pattern: regex.source)
69
+ # => <input type="text" id="site_url" name="site[url]" pattern="[javascript URL regex with optional scheme]" />
70
+
61
71
` UrlRegex.get ` returns regular Ruby's [ Regex] ( http://ruby-doc.org/core-2.0.0/Regexp.html ) object,
62
72
so you can use it as usual.
63
73
@@ -66,18 +76,18 @@ All regexes are case-insensitive.
66
76
## FAQ
67
77
68
78
Q: Hey, I want to parse HTML, but it doesn't work:
69
-
79
+
70
80
str = '<a href="http://google.com?t=1">Link</a>'
71
81
str.scan(UrlRegex.get(mode: :parsing))
72
82
# => "http://google.com?t=1">Link</a>"
73
-
74
- A: Well, you probably know that parsing HTML with regex is
83
+
84
+ A: Well, you probably know that parsing HTML with regex is
75
85
[ a bad idea] ( https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags ) .
76
86
It requires matching corresponding open and close brackets, that makes the regex even more complicated.
77
87
78
88
Q: How can I speed up processing?
79
89
80
- A: Generated regex depends only on options, so you can get the regex only once and cache it.
90
+ A: Generated regex depends only on options, so you can get the regex only once and cache it.
81
91
82
92
## Contributing
83
93
0 commit comments