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

有用的链接 (Useful links)

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 好汉字符串

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

  1. java正则表达式所有字符串_如何在Java中使用正则表达式打印字符串的所有字符?...

    元字符"." 匹配所有字符,以使用正则表达式打印所有字符-使用compile()方法编译正则表达式. 使用matcher()方法创建Matcher对象. 使用find()方法找到匹 ...

  2. mysql 查询重复字符串_如何在mysql中查询重复的字符串条目

    如果你以相反的顺序存储(8)最右边的字符,那么 您的表格将包含以下字段: id (int)| name (string) | phone (string) | phonerev (string) -- ...

  3. java+script+当前日期_如何在JavaScript中获取当前日期?

    如何在JavaScript中获取当前日期? #1楼 您可以使用扩展了 Date对象的Date.js库,从而可以使用.today()方法. #2楼 如果您想对日期格式进行更多的粒度控制,我强烈建议您查看 ...

  4. javascript案例_如何在JavaScript中使用增强现实-一个案例研究

    javascript案例 by Apurav Chauhan 通过Apurav Chauhan 如何在JavaScript中使用增强现实-一个案例研究 (How to use Augmented Re ...

  5. javascript编写_如何在JavaScript中使用解构来编写更简洁,功能更强大的代码

    javascript编写 by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用解构来编写更简洁,功能更强大的代码 (How to ...

  6. java 格式化 浮点数_如何在javascript中格式化浮点数?

    回答(13) 2 years ago 我猜的关键是首先正确地向上舍入,然后你可以将它转换为String . function roundOf(n, p) { const n1 = n * Math.p ...

  7. python中用什么函数读取字符串_如何在Python中获得函数名作为字符串?

    在Python中,如何在不调用函数的情况下以字符串的形式获得函数名? 1 2 3 4def my_function(): pass print get_function_name_as_string( ...

  8. 取出url中的字符_如何在JavaScript中解析URL:例如主机名,路径名,查询,哈希?...

    统一资源定位符(缩写URL)是对Web资源(网页,图像,文件)的引用.URL指定资源位置和检索资源的机制(http,ftp,mailto). 例如,这是此博客文章的URL: 通常,您需要访问URL的特 ...

  9. javascript字典中添加数组_如何在JavaScript中使用数组方法:Mutator方法

    JavaScript中的数组由元素列表组成.JavaScript有许多有用的内置方法来处理数组.修改原始数组的方法称为mutator方法,返回新值或表示的方法称为accessor方法.在本教程中,我们 ...

最新文章

  1. Apache Tiles 学习(四)、Tiles实战
  2. 一文概述 2018 年深度学习 NLP 十大创新思路
  3. linux kernel同步方法的总结
  4. math.sqrt 有问题_JavaScript中带有示例的Math.SQRT2属性
  5. 从各位前辈手中搜集的经验
  6. 开源数据屏蔽 数据加密_数据屏蔽或更改行为信息
  7. 同期两篇 Nature:运行温度高于 1K 的量子计算平台问世!
  8. 韩国李世石跟韩国AI大战,2负一胜
  9. 非参数检验统计量分析
  10. 金蝶14.0系统服务器安装教程,金蝶kis专业版14.0安装注意事项
  11. 旅游网页设计 web前端大作业 全球旅游私人订制 旅游公司网站模板(HTML+CSS+JavaScript)
  12. 人工智能十大发展方向
  13. c语言小游戏跳一跳代码及注释,c语言小游戏程序之弹跳小球的实现代码
  14. 「硬见小百科」几种镜像恒流源电路分析
  15. 外星人显卡拓展坞支持linux,今天,你给信仰充值了么?ALIENWARE 外星人 显卡扩展坞 简单开箱...
  16. 【架构】需求决定架构 —— 萌Mark的架构升级之路
  17. 为什么一般的眼科医院很难发现眼底疾病?这个是关键!
  18. 7个月时间“从零到亿”,社交电商靠谱好物为何总能占据行业“C位”?
  19. 微信小程序IOS sticky 兼容写法
  20. charles的简单使用

热门文章

  1. 【js】通过js代码改变html表单中的数据
  2. 微信表白墙 微信小程序 吐槽墙 表白墙 java 开发
  3. 如何给HTML添加事件?
  4. Redux 入门教程(三):React-Redux 的用法
  5. JS 实现下载Blod文件
  6. React typescript issue
  7. 《JavaScript应用程序设计》一一2.3 lambdas
  8. 启用CORS实现Ajax跨域请求
  9. maven基础概念学习1
  10. raspberry pi下使用mp3blaster播放mp3音乐