“? ' - 正则表达式 - js [英] '? ' - Regular expression - js
问题描述
let american = "color";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true -- why?
我尝试过:
了解并尝试另一个例子。但仍然,我什么都不懂
What I have tried:
to understand and try another examples. but still, i understand nothing
推荐答案
因为'?'就在它自己的前面(前面没有'*'或'+')说零或一上一个元素。
所以colou?r匹配颜色或颜色而不是colotr。
正如我上次建议的那样,获取 Expresso 的副本[ ^ ] - 它是免费的,它检查并生成正则表达式。
它会向你解释这个!
Because '?' on it's own (without '*' or '+' in front of it) says "zero or one" of the preceding element.
So "colou?r" matches either "color" or "colour" but not "colotr".
As I suggested last time, get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
It would have explained this to you!
你在正则表达式中是可选的,如?
所以'颜色'和'颜色'都是将匹配。
键入colou?进入正则表达式框,在这里查看:
< a href =https://www.debuggex.com/> Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
The u is optional in the regex, as denoted by the ?
So both 'color' and 'colour' will match.
Type colou?r in to the regex box on here to see:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
'? ' - 正则表达式 - js
'? ' - Regular expression - js
建议:阅读文档。
以下是RegEx文档的链接:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:Learn,Build,&测试RegEx [ ^ ]
在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]
这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用: Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
此网站还在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容: Regexper [ ^ ]
Advice: read documentation.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
这篇关于“? ' - 正则表达式 - js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!