README.md in url_regex-0.0.2 vs README.md in url_regex-0.0.3
- old
+ new
@@ -1,20 +1,20 @@
[![Build Status](https://travis-ci.org/amogil/url_regex.svg?branch=master)](https://travis-ci.org/amogil/url_regex)
[![Code Climate](https://codeclimate.com/github/amogil/url_regex/badges/gpa.svg)](https://codeclimate.com/github/amogil/url_regex)
-[![GitHub version](https://badge.fury.io/gh/amogil%2Furl_regex.svg)](https://badge.fury.io/gh/amogil%2Furl_regex)
+[![Gem Version](https://badge.fury.io/rb/url_regex.svg)](https://badge.fury.io/rb/url_regex)
[![Dependency Status](https://gemnasium.com/badges/github.com/amogil/url_regex.svg)](https://gemnasium.com/github.com/amogil/url_regex)
# UrlRegex
Provides the best known regex for validating and extracting URLs.
-It uses amazing job done by [Diego Perini](https://gist.github.com/dperini/729294)
+It builds on amazing work done by [Diego Perini](https://gist.github.com/dperini/729294)
and [Mathias Bynens](https://mathiasbynens.be/demo/url-regex).
-Why do we need a gem for this regex?
+Why do we need a gem for this regex?
-- You don't need to watch changes and improvements of original regex.
-- You have an ability to slightly customize the regex: a scheme can be optional, can get the regex for validation or parsing.
+- You don't need to follow changes and improvements of original regex.
+- You can slightly customize the regex: a scheme can be optional, and you can get the regex for validation or parsing.
## Installation
Add this line to your application's Gemfile:
@@ -31,58 +31,68 @@
## Usage
Get the regex:
UrlRegex.get(options)
-
+
where options are:
- `scheme_required` indicates that schema is required, defaults to `true`.
-- `mode` can gets either `:validation` or `:parsing`, defaults to `:validation`.
+- `mode` can gets either `:validation`, `:parsing` or `:javascript`, defaults to `:validation`.
`:validation` asks to return the regex for validation, namely, with `\A` prefix, and with `\z` postfix.
That means, it matches whole text:
UrlRegex.get(mode: :validation).match('https://www.google.com').nil?
# => false
UrlRegex.get(mode: :validation).match('link: https://www.google.com').nil?
# => true
-
+
`:parsing` asks to return the regex for parsing:
str = 'links: google.com https://google.com?t=1'
str.scan(UrlRegex.get(mode: :parsing))
# => ["https://google.com?t=1"]
-
+
# schema is not required
str.scan(UrlRegex.get(scheme_required: false, mode: :parsing))
# => ["google.com", "https://google.com?t=1"]
+`: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.
+
+ regex = UrlRegex.get(mode: :javascript)
+ text_field(:site, :url, pattern: regex.source)
+ # => <input type="text" id="site_url" name="site[url]" pattern="[javascript URL regex]" />
+
+ regex = UrlRegex.get(scheme_required: false, mode: :javascript)
+ text_field(:site, :url, pattern: regex.source)
+ # => <input type="text" id="site_url" name="site[url]" pattern="[javascript URL regex with optional scheme]" />
+
`UrlRegex.get` returns regular Ruby's [Regex](http://ruby-doc.org/core-2.0.0/Regexp.html) object,
so you can use it as usual.
All regexes are case-insensitive.
## FAQ
Q: Hey, I want to parse HTML, but it doesn't work:
-
+
str = '<a href="http://google.com?t=1">Link</a>'
str.scan(UrlRegex.get(mode: :parsing))
# => "http://google.com?t=1">Link</a>"
-
-A: Well, you probably know that parsing HTML with regex is
+
+A: Well, you probably know that parsing HTML with regex is
[a bad idea](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags).
It requires matching corresponding open and close brackets, that makes the regex even more complicated.
Q: How can I speed up processing?
-A: Generated regex depends only on options, so you can get the regex only once and cache it.
+A: Generated regex depends only on options, so you can get the regex only once and cache it.
## Contributing
1. Fork it ( https://github.com/[my-github-username]/url_regex/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
-5. Create a new Pull Request
\ No newline at end of file
+5. Create a new Pull Request