This article was peer-reviewed by Sebastian Seitz and Almir Bijedic. Thanks to all the peer reviewers at SitePoint for getting SitePoint content to its best!
Almost every programmer needs to use regular expressions in some form from time to time. For many, pattern grammar may seem mysterious and daunting. This tutorial will introduce a new pattern matching engine apg-exp - a feature-rich alternative to RegExp, which uses ABNF pattern syntax and is easier to read.
Have you ever needed to verify your email address and encountered something like this?
<code>^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$</code>
The pattern matching engine is the right tool for this job. This is a well-designed, well-written regular expression. It works fine. So what else do you don’t like?
Well, if you are an expert in regular expressions, there is nothing. But for the rest of us, they might be
Regular expression grammar has a long and long history and has been deeply integrated into many of the tools and languages we use as programmers every day.
However, there is an alternative syntax that has been around for almost the same time, which is very popular among writers and consumers of Internet technical specifications, with all the functions of regular expressions, but is rarely in the JavaScript programming world use. That is, the enhanced Bacos-Nor paradigm (ABNF), formally defined by the IETF in RFC 5234 and RFC 7405.
Let's see what the same email address looks like in ABNF.
<code>email-address = local "@" domain local = local-word *("." local-word) domain = 1*(sub-domain ".") top-domain local-word = 1*local-char sub-domain = 1*sub-domain-char top-domain = 2*6top-domain-char local-char = alpha / num / special sub-domain-char = alpha / num / "-" top-domain-char = alpha alpha = %d65-90 / %d97-122 num = %d48-57 special = %d33 / %d35 / %d36-39 / %d42-43 / %d45 / %d47 / %d61 / %d63 / %d94-96 / %d123-126</code>
Of course, it's not compact, but like HTML and XML, it's designed to be read by humans and machines. I guess, just a little bit of understanding of wildcard search patterns, you can almost read what's going on here in "simple English".
This email address for RegExp and apg-exp is compared in Example 1.
apg-exp is a pattern matching engine designed to have the look and feel of RegExp, but uses ABNF syntax for pattern definition. In the next few sections, I will guide you through:
If you are working in a Node.js environment, run from your project directory:
<code>^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$</code>
You can then access it in your code using require().
Example:
<code>email-address = local "@" domain local = local-word *("." local-word) domain = 1*(sub-domain ".") top-domain local-word = 1*local-char sub-domain = 1*sub-domain-char top-domain = 2*6top-domain-char local-char = alpha / num / special sub-domain-char = alpha / num / "-" top-domain-char = alpha alpha = %d65-90 / %d97-122 num = %d48-57 special = %d33 / %d35 / %d36-39 / %d42-43 / %d45 / %d47 / %d61 / %d63 / %d94-96 / %d123-126</code>
To get a copy of the code from GitHub, you can clone the repository to your project directory:
<code>npm install apg-exp --save</code>
or download it as a zip file.
Then in page.html:
<code>var ApgExp = require("apg-exp"); var exp = new ApgExp(pattern, flags); var result = exp.exec(stringToMatch);</code>
You can also use RawGit to create CDN versions directly from GitHub source code. However, be sure to read No uptime or support guarantee (actually be sure to read the entire FAQ).
All examples in this tutorial use the following.
<code>git clone https://github.com/ldthomas/apg-js2-exp.git apg-exp</code>
These files are cached on the MaxCDN server and you can use them to test as long as they are available. However, for production environments, you should place copies of apgexp-min.js and apgexp.css on your own server to ensure access and include them in your page according to the best way for your application .
(The following content has been truncated due to the length of the article. Please provide the subsequent part for continued processing)
The above is the detailed content of An Alternative to Regular Expressions: apg-exp. For more information, please follow other related articles on the PHP Chinese website!