最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress - use of regular expression in 'contains' function returning no match - Stack Overflow

programmeradmin1浏览0评论

Cypress is supposed to allow regular expressions in its 'contains' function, but it is finding no match, whereas there are a lot of elements containing the search text. I have to put in the full name to allow it to find it - but I want to get all the elements with names starting with 'Tier2_Forest' not just one.

For instance, this works:

cy.get('#database-col')
  .find('div.el-row div.card header.card-header p.card-header-title')
  .contains('Tier2_Forest : Tier2_Forest_ManEvents')
  .as('dbCard');

But this doesn't:

cy.get('#database-col')
  .find('div.el-row div.card header.card-header p.card-header-title')
  .contains('Tier2_Forest')
  .as('dbCard');

Have also unsuccessfully tried it in the format remended by Cypress for regex - i.e. line starts with that phrase: contains(/^Tier2_Forest/)

For some reason when I use contains('.db') it returns a list of elements containing '.db' at the end of their names - but I don't want those... Snippet of the generated DOM:


[]

Cypress is supposed to allow regular expressions in its 'contains' function, but it is finding no match, whereas there are a lot of elements containing the search text. I have to put in the full name to allow it to find it - but I want to get all the elements with names starting with 'Tier2_Forest' not just one.

For instance, this works:

cy.get('#database-col')
  .find('div.el-row div.card header.card-header p.card-header-title')
  .contains('Tier2_Forest : Tier2_Forest_ManEvents')
  .as('dbCard');

But this doesn't:

cy.get('#database-col')
  .find('div.el-row div.card header.card-header p.card-header-title')
  .contains('Tier2_Forest')
  .as('dbCard');

Have also unsuccessfully tried it in the format remended by Cypress for regex - i.e. line starts with that phrase: contains(/^Tier2_Forest/)

For some reason when I use contains('.db') it returns a list of elements containing '.db' at the end of their names - but I don't want those... Snippet of the generated DOM:


[]

Share Improve this question edited May 13, 2021 at 2:17 user14783414 asked Jun 4, 2020 at 2:04 MillturnKMillturnK 911 gold badge1 silver badge6 bronze badges 1
  • I nowhere see Tier2_Forest_ManEvents in that picture (text would be better), only Tier2_Forest Man Events. On top that represents the rendered DOM, not the actual source of the page - those things can still differ. – AmigoJack Commented Jun 4, 2020 at 8:13
Add a ment  | 

2 Answers 2

Reset to default 2

I would say you are on the right track, but .contains(/^Tier2_Forest/) fails because the text is not at the beginning of the string, there is white space preceding it.

Just try .contains(/Tier2_Forest/)

or more precisely .contains(/\s*Tier2_Forest/)

where \s* matches any whitespace character (equal to [\r\n\t\f\v ])

Check it out in https://regex101.

I had the same problem and I solved it like this:
It takes into account spaces and line breaks if there are any.

.contains(/^\s*\n?Tier2_Forest/) // check begins, with nothing before
.contains(/^\s*\n?Tier2_Forest : Forest Database\s*\n?$/) //exact same sentence, check nothing before, check nothing after
发布评论

评论列表(0)

  1. 暂无评论