我需要在 Cucumber js 步骤中根据正则表达式捕获值
用例 =>
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”。请任何人提出建议
答案 0 :(得分: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
读起来更像是你真的会怎么写这句话。