Home > Web Front-end > JS Tutorial > An Alternative to Regular Expressions: apg-exp

An Alternative to Regular Expressions: apg-exp

William Shakespeare
Release: 2025-02-17 11:25:18
Original
1096 people have browsed it

An Alternative to Regular Expressions: apg-exp

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.

Key Points

  • apg-exp provides a user-friendly alternative to regular expressions, using ABNF syntax, which is easier to read and understand for users who are not familiar with the traditional RegExp pattern.
  • apg-exp is very simple to install and use, and supports the Node.js environment and GitHub direct download options, allowing it to access various project settings.
  • ABNF syntax used by apg-exp breaks complex patterns into simpler, more readable components, which is more intuitive than the usually mysterious regular expressions.
  • apg-exp supports advanced pattern matching features that are not available in RegExp in JavaScript, such as recursion, which is critical to matching nested patterns.
  • This library provides detailed error handling and debugging tools to help developers effectively identify and resolve problems in pattern matching logic.
  • Despite its powerful capabilities, apg-exp still maintains a simple API, making it easy to integrate and use without the need for a lot of modifications to existing code bases.

Quick comparison

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>
Copy after login
Copy after login

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

  • Hard to read
  • It's harder to write
  • Difficult to maintain

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>
Copy after login
Copy after login

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".

  • Email address is defined as local parts and domains separated by @
  • The local part is a word followed by an optional dot-separated word
  • Domains are one or more dot-separated subdomains followed by a single top-level domain
  • The only thing you may not know here, but what you may have guessed is:
    • Just as the wildcard character means "zero or more", 1 means "one or more", while 2*6 means minimum 2 times and maximum 6 repetitions
    • / Separate Alternatives
    • %d Defines decimal character code and character code range
    • For example, �5 means #, ASCII decimal 35
    • �5-90 means any character in the A-Z range, ASCII decimal 65-90

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:

  • How to integrate apg-exp into your application
  • A brief guide to ABNF syntax
  • Use apg-exp—some examples
  • Where to go next - More details, advanced examples

Upload and run-How to get it

npm

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

GitHub

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>
Copy after login

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>
Copy after login

CDN

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>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template