博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾
阅读量:2520 次
发布时间:2019-05-11

本文共 5855 字,大约阅读时间需要 19 分钟。

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 ’s Basic Algorithm Scripting “”.

本文基于的基本算法脚本“ ”。

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”标志。

in MDN

MDN中的

in MDN

MDN中的

in MDN

MDN中的

in

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

The challenge “” 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 in .

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

This ad-hoc RegExp solution can also help you solve the freeCodeCamp Intermediate Algorithm Scripting “” 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 .

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

翻译自:

regexp 好汉字符串

转载地址:http://ptuzd.baihongyu.com/

你可能感兴趣的文章
hdu 1735(贪心) 统计字数
查看>>
iOS 系统框架结构图
查看>>
uml系列(六)——行为图:活动&状态
查看>>
Learning Deconvolution Network for Semantic Segme小结
查看>>
Leetcode 424.替换后的最长重复字符
查看>>
第二阶段:2.商业需求文档MRD:1.M版本管理
查看>>
我爱Java系列---【单列集合和双列集合总结】
查看>>
新开始
查看>>
git - 如何从项目中删除git跟踪
查看>>
MacBook Air密码忘了,苹果电脑密码忘了怎么办
查看>>
PHP二维数组排序
查看>>
.Net Core WebApi返回的json数据,自定义日期格式
查看>>
C语言运算符表
查看>>
网络调试 adb connect
查看>>
ormlite 文档
查看>>
修改root远程ssh登录权限
查看>>
保存cookies
查看>>
iOS酷炫动画效果合集
查看>>
[CSS] Scale on Hover with Transition
查看>>
状压DP(挑战程序设计竞赛)
查看>>