
Security News
Rspack Introduces Rslint, a TypeScript-First Linter Written in Go
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
Provides the best known regex for validating and extracting URLs. It builds on amazing work done by Diego Perini and Mathias Bynens.
Why do we need a gem for this regex?
Add this line to your application's Gemfile:
gem 'url_regex'
And then execute:
$ bundle
Or install it yourself as:
$ gem install url_regex
Get the regex:
UrlRegex.get(options)
where options are:
scheme_required
indicates that schema is required, defaults to true
.
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 object,
so you can use it as usual.
All regexes are case-insensitive.
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 bad idea. 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.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that url_regex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.