regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾

regexp 好汉字符串

by Catherine Vassant (aka Codingk8)

由凯瑟琳·瓦森(Catherine Vassant)(又名Codingk8)

如何在JavaScript中使用RegExp确认字符串的结尾 (How to use a RegExp to confirm the ending of a String in JavaScript)

Using the Regexp ?️ constructor

使用Regexp?️构造函数

This article is based on freeCodeCamp’s Basic Algorithm Scripting “Confirm the ending”.

本文基于freeCodeCamp的基本算法脚本“ 确认结尾 ”。

This challenge involves checking whether a String ends with a specific sequence of letters or not.

这个挑战涉及检查字符串是否以特定的字母序列结尾。

In this article, I’ll explain how to solve this challenge using a RegExp.

在本文中,我将解释如何解决此问题 使用RegExp挑战。

The interesting aspect of this solution is using the RegExp constructor to create the specific RegExp you need to check Strings passed as arguments.

此解决方案有趣的方面是使用RegExp构造函数来创建特定的RegExp,您需要检查作为参数传递的字符串。

算法挑战 (Algorithm Challenge)

Check if a string (first argument, str) ends with the given target string (second argument, target).

检查字符串(第一个参数, str )是否以给定的目标字符串(第二个参数, target )结尾。

This challenge can be solved with the .endsWith()method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.

可以通过.endsWith()中引入的.endsWith()方法解决此挑战。 但是出于此挑战的目的,我们希望您改用一种JavaScript子字符串方法。

提供的测试用例 (Provided test cases)

confirmEnding("Bastian", "n")should return true.

confirmEnding("Bastian", "n")应该返回true。

confirmEnding("Congratulation", "on")should return true.

confirmEnding("Congratulation", "on")应该返回true。

confirmEnding("Connor", "n")should return false.

confirmEnding("Connor", "n")应该返回false。

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")应返回false。

confirmEnding("He has to give me a new name", "name")should return true.

confirmEnding("He has to give me a new name", "name")应该返回true。

confirmEnding("Open sesame", "same")should return true.

confirmEnding("Open sesame", "same")应该返回true。

confirmEnding("Open sesame", "pen")should return false.

confirmEnding("Open sesame", "pen")应返回false。

confirmEnding("Open sesame", "game")should return false.

confirmEnding("Open sesame", "game")应该返回false。

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")应该返回false。

confirmEnding("Abstraction", "action")should return true.

confirmEnding("Abstraction", "action")应该返回true。

Do not use the built-in method .endsWith()to solve the challenge.

不要使用内置方法.endsWith()解决挑战。

1.第一个根本不起作用的想法 (1. The first idea that does not work at all)

If, like me, you’re a RexExp lover, your first attempt might be to try solve the challenge with the code below, and it won’t work.

如果像我一样,您是RexExp的爱好者,那么您的第一个尝试可能是尝试使用下面代码来解决挑战,但它将不起作用

The reason is, with this syntax, the test() function will look for the specific “target” String and not “target” as a variable passed as an argument.

原因是,使用这种语法,test()函数将查找特定的“目标”字符串,而不是“目标”作为作为参数传递的变量。

If we go back to our test cases, the ones that should return “false”, do pass, but none of the ones that should return “true” pass, which is quite predictable.

如果我们回到测试用例,应该返回“ false”的测试通过,但是没有返回“ true”的测试通过,这是可以预见的。

2.通过使用RegExp构造函数创建所需的特定RegExp来解决挑战 (2. Solve the challenge by creating the specific RegExp you need with the RegExp constructor)

In order to use a RegExp that is going to “understand” that the “target” argument is a variable and not the String “target”, you have to create a taylor-made RegExp using the RegExp constructor.

为了使用RegExp来“理解”“ target”参数是一个变量而不是字符串“ target”,您必须使用RegExp构造函数创建一个定制的RegExp

And, before we move forward, let’s go back for a minute and look at what we want to test: the “target” argument should be the ending of the “str” argument. This means our RegExp should end with the “$” character.

而且,在继续前进之前,让我们先回头看一下我们要测试的内容:“ target”自变量应该是“ str”自变量的结尾。 这意味着我们的RegExp应该以“ $”字符结尾

现在,我们可以分三步解决这个挑战 (Now, we can solve this challenge in three steps)

Step 1 - Create a variable adding the “$” at the end of the “target” argument, using the concat() method in this case.

步骤1-在这种情况下,使用concat()方法创建一个变量,在“ target”参数的末尾添加“ $”。

Step 2 - Use the RegExp constructor and the “new” operator to create the right RexExp with the above variable.

第2步 -使用RegExp构造函数和“ new”运算符使用上述变量创建正确的RexExp。

Step 3 - Return the result of the test() function.

第3步 -返回test()函数的结果。

And this passes all the case tests beautifully ?

这能通过所有案例测试吗?

可以像这样在两行中重构 (This can be refactored in two lines like this)

Note: since none of the test cases imply to test the capitalization of the letters, there’s no need to use the “i” flag.

注意 :由于所有测试用例均未暗示要测试字母的大写,因此无需使用“ i”标志。

String.prototype.concat() in MDN

MDN中的String.prototype.concat()

RegExp.prototype.test() in MDN

MDN中的RegExp.prototype.test()

RegExp constructor in MDN

MDN中的RegExp构造函数

Regular Expressions in freeCodeCamp

freeCodeCamp中的 正则表达式

其他解决方案 (Other solutions to this challenge)

The challenge “Get a Hint suggests a solution using the slice() method.

挑战“ 获得提示提出了使用slice()方法的解决方案。

You can find two other ways of solving this challenge, one with the substr() method and the other with the endsWith() method, explained by Sonya Moisset in this article.

您可以找到其他两种方法来解决此难题,一种方法是使用substr()方法 ,另一种方法是使用endsWith()方法, 本文Sonya Moisset解释

This ad-hoc RegExp solution can also help you solve the freeCodeCamp Intermediate Algorithm Scripting “Search and Replace” challenge.

此临时RegExp解决方案还可以帮助您解决freeCodeCamp中级算法脚本“ 搜索和替换 ”挑战

Thank you for reading!

感谢您的阅读!

If you enjoyed this article, please “hands-clap” as many times as you like and share it to help other people find it. That may make their day.

如果您喜欢这篇文章, 请随意“拍手”多次并分享以帮助其他人找到它。 那可能会成功。

If you have a reaction/question/suggestion, be sure to leave a comment below. I’ll be glad to read from you!

如果您有任何React/问题/建议 ,请务必在下面发表评论 。 我很高兴收到您的来信!

You can also get in touch and/or follow me on Twitter.

您也可以在Twitter上取得联系和/或关注

翻译自: https://www.freecodecamp.org/news/how-to-use-a-regexp-to-confirm-the-ending-of-a-string-in-javascript-4b42f3749af1/

regexp 好汉字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值