qat*_*123 7 html javascript ascii removing-whitespace cypress
我正在尝试从代表金钱的数值中删除空格。
例如,我想1 000 kr成为1000。当货币为10 000
我正在使用此函数来删除,kr但是当我尝试添加另一个函数时.replace它不起作用:
Cypress.Commands.add('currency', (selector) => {
cy.get(selector)
.children()
.eq(1)
.invoke('text') // get text
.then((text) => +text.replace('kr', '').trim());
});
Run Code Online (Sandbox Code Playgroud)
小智 6
这应该有效:
function formatString(text) {
return text.replace('kr', '').replace('\u00A0','').trim();
}
Cypress.Commands.add('currency', (selector) => {
cy.get(selector)
.children()
.eq(1)
.invoke('text') // get text
.then(formatString)
});
Run Code Online (Sandbox Code Playgroud)
但您也可以使用 Cypress 使用正则表达式字符串来实现此目的(Cypress 常见问题解答中的第一个示例与您的示例类似):
Cypress.Commands.add('currency', (selector) => {
cy.get(selector)
.should('have.text', '\d+[\u00A0]*')
});
Run Code Online (Sandbox Code Playgroud)
您可以在此处测试正则表达式: https: //regex101.com/r/YC4szy/1。它将匹配带有数字、后跟空格以及任何后续字符的字符串。您可以使用正则表达式来测试您想要的内容。
我的最后一个建议是,如果匹配正则表达式模式没有帮助,您可以将 cypress 命令包装在一个函数中,该函数将文本内容作为参数,并将其传递到该should('have.text', ...)行中。
| 归档时间: |
|
| 查看次数: |
22025 次 |
| 最近记录: |