Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从Cumber步骤中捕获正则表达式_Javascript_Regex_Gherkin_Cucumberjs - Fatal编程技术网

Javascript 如何从Cumber步骤中捕获正则表达式

Javascript 如何从Cumber步骤中捕获正则表达式,javascript,regex,gherkin,cucumberjs,Javascript,Regex,Gherkin,Cucumberjs,我需要在每个步骤中捕获基于正则表达式的值 用例=> When I click the '2nd' item in the list 我想在上面的步骤定义中捕捉“2” 我在步骤定义=> When(/^I click the '\d{1,2}(?:st|nd|rd|th)?' item in the list$/, async function(element) { await helperActions.clickTopBanksListElement(element); }); 但无

我需要在每个步骤中捕获基于正则表达式的值

用例=>

When I click the '2nd' item in the list
我想在上面的步骤定义中捕捉“2”

我在步骤定义=>

When(/^I click the '\d{1,2}(?:st|nd|rd|th)?' item in the list$/, async function(element) {
    await helperActions.clickTopBanksListElement(element);
});

但无法捕获“2”。任何人,请建议

你只需要用括号捕捉数字:
'(\d{1,2})(?:st | nd | rd | th)'

此外,您不需要将数字限制为一个或两个字符。使用
(\d+)
将捕获一个或多个数字。我也不知道您为什么试图在st、nd、rd和th缩写的反向引用开头包含
?:
字符

When(/^I click the '(\d+)(st|nd|rd|th)?' item in the list$/, async function(element) {
    await helperActions.clickTopBanksListElement(element);
});
或者,您不需要引号:

When(/^I click the (\d+)(st|nd|rd|th)? item in the list$/, async function(element) {
    await helperActions.clickTopBanksListElement(element);
});
给你:

When I click the 2nd item in the list

这看起来更像是你如何写这个句子。

你只需要用括号捕捉数字:
'(\d{1,2})(?:st | nd | rd | th)?'

此外,您不需要将数字限制为一个或两个字符。使用
(\d+)
将捕获一个或多个数字。我也不知道您为什么试图在st、nd、rd和th缩写的反向引用开头包含
?:
字符

When(/^I click the '(\d+)(st|nd|rd|th)?' item in the list$/, async function(element) {
    await helperActions.clickTopBanksListElement(element);
});
或者,您不需要引号:

When(/^I click the (\d+)(st|nd|rd|th)? item in the list$/, async function(element) {
    await helperActions.clickTopBanksListElement(element);
});
给你:

When I click the 2nd item in the list

你可以使用一个捕获组
(\d{1,2})
对,
给定(/^I单击列表中的“(\d{1,2})(?:st | nd | rd | th”)项($/,(item)=>{…})
`您可以使用捕获组
(\d{1,2})
对,
给定(/^I单击列表$/,(item)=>{…}中的“(\d{1,2})(?:st | nd | rd | th)项)`