介绍(Introduction)
I really enjoy regular expressions and I find that for a lot of the algorithm challenges on leetcode or codewars, you can come up with some interesting solutions for problems using regular expressions. Or they are great for creating form validators or whatever way in which you may need to check for a string of characters, numbers, and even special characters. If you are not familiar with what regular expressions are, then this blog post is for you.
我真的很喜欢正则表达式,而且我发现对于leetcode或codewar上的许多算法挑战,您可以为使用正则表达式的问题提出一些有趣的解决方案。 或者,它们非常适合创建表单验证器或您可能需要检查字符串,数字甚至特殊字符的任何方式。 如果您不熟悉什么是正则表达式,那么此博客文章适合您。
Regular Expressions, or RegEx for short, allow us to check for a match of a series of characters. For instance, you might find yourself needing to only find vowels in a string, well you can use RegEx to match ‘a’,‘e’,‘i’,‘o’,‘u’ and you can even have flexibility when doing so. You could potentially either pull all instances of each vowel’s occurrence or when they first occur or only match for them if they happen in succession. The possibilities are up to you which is great!
正则表达式(简称RegEx)使我们能够检查一系列字符是否匹配。 例如,您可能会发现自己只需要查找字符串中的元音,那么您可以使用RegEx来匹配“ a”,“ e”,“ i”,“ o”,“ u”,甚至在执行时也可以具有灵活性。所以。 您可能会提取每个元音出现的所有实例或它们首次出现时的所有实例,或者只有在它们连续出现时才匹配它们。 可能性取决于您,这太棒了!
There are multiple ways in which you can test for matches. Two ways I will show in this blog post are the .test()
method and the .match()
method. Let’s look at an example so that you can get a better idea of what this would actually look like.
您可以通过多种方式测试匹配项。 我将在此博客文章中显示两种方法: .test()
方法和.match()
方法。 让我们看一个示例,以便您可以更好地了解其实际外观。
For the sake of this post, I will not add any flags in the RegEx examples, but I will talk about them in an upcoming blog post.
为了这篇文章的缘故,我不会在RegEx示例中添加任何标志,但是我将在即将发表的博客文章中讨论它们。
码 (Code)
。测试()(.test())
The .test()
method takes one argument:
- String 串
You first write out the RegEx pattern that you are trying to match for and then in parentheses, you will put the string that you will test for.
您首先要写出要匹配的RegEx模式,然后在括号中放入要测试的字符串。
The return value will be a boolean true or false if the pattern is found or not.
如果找不到模式,则返回值将为布尔值true或false。
Here is an example if we want to test our string has vowels or not:
这是一个示例,如果我们要测试我们的字符串是否带有元音:
。比赛() (.match())
The second way to test for patterns is to actually extract out the patterns that you are looking for. We can do this using the .match()
method. It takes one argument:
测试模式的第二种方法是实际提取所需的模式。 我们可以使用.match()
方法做到这一点。 它有一个参数:
- RegEx 正则表达式
You will use the .match() as a property on your string value that you are testing and inside of the parentheses, you will put your RegEx pattern. The return value will be an array of all the matches or an empty array. The returned array will be dependent on the ‘g’ flag which I will mention in the next blog, but essentially means ‘global’. It is returning all occurrences of the pattern you want to match.
您将使用.match()作为要测试的字符串值的属性,并在括号内,将放置RegEx模式。 返回值将是所有匹配项组成的数组或空数组。 返回的数组将取决于我将在下一个博客中提到的'g'标志,但实际上意味着'global'。 它返回所有要匹配的模式。
Here we will use the same example as above but with the .match method. As you can see it returns an array and the first index is the vowel it matched, along with its index inside the string we were testing and the input string.
在这里,我们将使用与上述相同的示例,但使用.match方法。 如您所见,它返回一个数组,第一个索引是它匹配的元音,以及它在我们测试的字符串和输入字符串中的索引。
结论 (Conclusion)
These are two ways to get started with RegEx, and while this was short, I just wanted it to be a short introduction into getting started and to understand why we might want to know how we could start using them in our code moving forward.
这是使用RegEx入门的两种方法,虽然简短,但我只是希望它是入门入门的简短介绍,并了解为什么我们可能想知道如何在今后的代码中开始使用它们。
翻译自: https://medium.com/@rclarke_m/regex-series-what-is-regex-926ea8284cd9