突然讨厌做前端,讨厌代码_你讨厌正则表达式吗? 那么,我为您提供解决方案......

突然讨厌做前端,讨厌代码

by Bukhari Muhammad

通过布哈里·穆罕默德(Bukhari Muhammad)

你讨厌正则表达式吗? 那么,我为您提供解决方案... (Do you hate regex? Well then, I have a solution for you…)

The following piece presents my latest package on NPM, which is also available on GitHub as a repository. I wrote it in hope that it be beneficial to all who may need it.

下一篇介绍了我关于NPM的最新软件包 ,该软件包也可以在GitHub上作为存储库使用 。 我写它是希望对所有可能需要它的人都有益。

那么...什么是PregX? (So… What is PregX?)

PregX, contrary to common belief, refers to: Popular Regex (Patterns), and not Perl Regular Expressions. Speaking of “common”, it is a library — written in JavaScript — that aims to be the largest collection of popular and common regular expressions.

与普遍的看法相反, PregX是指: 流行的正则表达式 (模式) ,而不是Perl正则表达式。 说到“通用”,它是一个用JavaScript编写的库,旨在成为最大的流行和通用正则表达式集合。

Developers unused to regular expressions may face difficulties when trying to use complex regular expression notations and syntax. This library should help these developers achieve common tasks that require regular expressions. It leverages the pre-written patterns that we most frequently use in our projects.

在尝试使用复杂的正则表达式符号和语法时,未使用正则表达式的开发人员可能会遇到困难。 该库应帮助这些开发人员完成需要正则表达式的常见任务。 它利用了我们在项目中最常用的预写模式。

为什么不编写自己的正则表达式? (Why not write your own regular expressions?)

The Don’t Repeat Yourself (DRY) approach is that repetition in code should be reduced via automation or abstraction. This is a fundamental principle of programming that ought to be obeyed whenever possible. Since duplication wastes both time and space, I went to some length to find a solution not just for myself, but for others as well.

请勿重做(DRY)方法是应通过自动化或抽象减少代码中的重复。 这是编程的基本原则,应尽可能遵守。 由于复制会浪费时间和空间,因此我不遗余力地找到了一个解决方案,不仅适用于我自己,也适用于其他人。

What’s the point behind the DRY principle if we keep reinventing bread every time a Tom, Dick or Harry discovers wheat?

如果我们每次汤姆,迪克或哈利发现小麦时都不断重新发明面包,那么干法原理的意义何在?

I found common regex patterns. My idea was to abstract each of these popular patterns into a useful pure function of its own. This abstraction would not mutate or alter the state of the given string to match. Each function would then return an array of the items matched in the given string.

我发现了常见的正则表达式模式。 我的想法是将这些流行的模式中的每一个抽象成自己有用的纯函数。 这种抽象不会改变或改变给定字符串的状态以使其匹配。 然后,每个函数将返回在给定字符串中匹配的项的数组。

PregX was not designed to oppose originality. It has two simultaneous aims. One aim is to reduce the time that developers spend on trying to use regex. The other aim is to avoid complexity where it does not belong.

PregX并非旨在反对原创性。 它有两个同时目标。 目的之一是减少开发人员花在尝试使用正则表达式上的时间。 另一个目的是避免不属于它的复杂性。

Though, guilty as charged, this package is also (indirectly) meant for those lazy-bums that always evade writing regular expressions.

虽然有罪,但此软件包也(间接)适用于那些总是逃避编写正则表达式的懒惰。

Then again, you may need to write your own regex. Your approach totally depends on the needs of the task at hand.

再一次,您可能需要编写自己的regex 。 您的方法完全取决于手头任务的需求。

什么时候应该使用PregX? (When should I use PregX?)

Good question, when should you?

好问题,你什么时候应该?

PregX can be used when inbuilt functionality fails to achieve, or is inadequate for, your complex desired outcome. Some might argue that regex could achieve this task.

当内置功能无法实现或不足以实现所需的复杂结果时,可以使用PregX 。 有人可能会说正则表达式可以实现此任务。

Regex might shorten the code effectively. However, it can also lessen the readability of your code, depending on the complexity of the task at hand.

正则表达式可以有效地缩短代码。 但是,这也可能会降低代码的可读性,具体取决于手头任务的复杂性。

Why not opt for a ready baked solution that’s abstracted into a useful function?

为什么不选择抽象为有用功能的现成烘焙解决方案?

This is where PregX comes into play. Patterns are abstracted in functions in line with the Functional Programming Paradigm. This makes it the go-to solution for common regex tasks. Using PregX achieves the same outcome, without settling for less readable code or reinventing bread.

这就是PregX发挥作用的地方。 模式是根据函数编程范式在函数中抽象的 这使其成为常见正则表达式任务的首选解决方案。 使用PregX可以达到相同的结果,而无需解决可读性较低的代码或重新设计面包的问题。

A broadly accurate principle is: Whatever pattern you’re cooking up, there’s probably a person who’s already written it. PregX takes this rule of thumb, by accumulating such patterns for the greater bunch of non-regex oriented developers.

一个大致准确的原则是:无论您采用哪种模式,都有可能已经有人在写它。 PregX通过积累大量面向非正则表达式的开发人员的模式来遵循这一经验法则。

Then again, this all depends on what you’re comfortable with. If you’re not hot at writing your own patterns then this is an easy way out.

再说一遍,这一切都取决于您是否满意。 如果您不热衷于编写自己的模式,那么这是一个简单的方法。

何时不使用PregX (When not to use PregX)

If you seek an out-of-the-ordinary , pattern that’s super specific to your needs, then PregX simply is not the answer. PregX is not intended to replace regex, that’d be super silly. Regex will always remain the best solution when it comes to precise or explicit tasks of this nature. Bottom-line, a good developer will know when to use a loaf tin and when to use a bakery.

如果您寻求与您的需求超级相关的非常规模式 ,那么PregX根本无法解决问题。 PregX 不是要取代regex ,那太傻了。 当涉及到这种性质的精确或明确任务时,正则表达式将始终是最佳解决方案。 归根结底,一个好的开发商会知道何时使用面包罐头以及何时使用面包店。

PregX的简单性和实用性 (The simplicity and usefulness of PregX)

I thought I’d showcase a few practical code samples to demonstrate the awesomeness of this library. That is, if you’re not yet convinced by all the boring talk I’ve written so far. ?

我以为我展示了一些实用的代码示例来演示该库的强大功能。 也就是说,如果您还不相信我到目前为止所写的所有无聊的谈话。 ?

信用卡示例 (Credit card example)

The following example shows how to match a credit card number using a string, with getCreditCardNumber(). One could even specify the type pf credit card to match, via the cardType property of the function’s config object. By default, this function matches Visa card numbers. Check this sample:

下面的示例演示如何使用字符串与getCreditCardNumber()匹配信用卡号。 甚至可以通过函数的config对象的cardType属性指定要匹配的pf信用卡类型。 默认情况下,此功能与Visa卡号匹配。 检查此样本:

Not impressed? I’ve got enough tricks up our sleeve to entice you.

没有留下深刻印象? 我有足够的技巧来吸引你。

邮政编码示例 (Postal code example)

If the above code sample didn’t do it, then this one will.

如果上面的代码示例没有做到这一点,那么这一点就可以了。

The pattern of the getPostalCode() function is among the many patterns that I’ve written myself. There are lots of solutions out there that attempt to do this. None match PregX’s standard.

getPostalCode()函数的模式是我自己编写的许多模式之一。 有很多解决方案试图做到这一点。 没有匹配的PregX 标准。

I’ve used Wikipedia’s Postal Code List as a reference, and cooked up PregX’s single greatest function. With support for matching the postal addresses of over 150 countries, this function — at least for me — remains the best solution to achieve this common task. Take a look:

我已将Wikipedia的邮政编码列表作为参考,并完善了PregX的最大功能。 在支持匹配150多个国家/地区的邮政地址的情况下 ,至少对于我来说,此功能仍然是实现此常见任务的最佳解决方案。 看一看:

当前支持的模式 (Currently Supported Patterns)

PregX supports a curated list of 35 (and counting)different patterns, all well tested to match their appropriate text. Here’s a list of the supported patterns:

PregX支持精选的35种(并正在计数)不同模式的列表,所有模式都经过了充分测试以匹配其适当的文本。 以下是受支持的模式的列表:

说服了吗 (Convinced?)

These are only two examples of the many patterns that this library contains. For documentation, examples of usage, and more information on PregX, check its repository on GitHub. Better yet, clone it or test it out by simply installing it via NPM:

这些只是该库包含的许多模式的两个示例。 有关文档,用法示例以及有关PregX的更多信息,请查看其在GitHub上的存储库 。 更好的是,只需通过NPM安装即可克隆或测试它:

npm install pregx --save

I’d love to hear your feedback on this package. PregX currently consists of 35 patterns, I hope it reaches ~100. So, feel free to contribute any useful patterns. Star it on git.

我很想听听您对这个包裹的反馈。 PregX目前包含35种模式,我希望它能达到100种。 因此,请随时贡献任何有用的模式。 在git上加注星标

This is my first open source project on NPM, but not my first on GitHub. Love it, fork it. Hate it, critique it. To get updates on the progress of this repository along with some cool JavaScript / React related tips, follow me on twitter: @bukharim96

这是我在NPM上的第一个开源项目,但不是我在GitHub上的第一个开源项目。 爱它, 叉子 。 讨厌它,批评它。 要获得有关此存储库进度的更新以及一些很棒JavaScript / React相关提示,请在Twitter上关注我: @ bukharim96

Thanks for reading this post.

感谢您阅读这篇文章。

Peace.

和平。

翻译自: https://www.freecodecamp.org/news/pregx-for-those-who-wish-to-dodge-regex-250e4a484ee0/

突然讨厌做前端,讨厌代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值