简体   繁体   English

如何从 cypress 中括号中的文本元素中提取文本,即测试(5)

[英]How to extract text from element having text in brackets in cypress i.e. test(5)

Can someone pls help in extracting text from element.有人可以帮助从元素中提取文本吗? Let us say we have element shown in page having text 'Test(10).假设我们在页面中显示了具有文本“Test(10)”的元素。 How we can get text as o/p Test and 10 separately.我们如何分别获得文本作为 o/p 测试和 10。

Thanks in advance!提前致谢!

You can use regex with name capture group.您可以将正则表达式与名称捕获组一起使用。

// example for 'Test(10)'
cy.get('.element-selector')
  .invoke('text')
  .then(elText => {
    const matcher = (?<text>test)\((?<number>\d+)\)
    // extracts 'Test'
    const text = elText.match(matcher)?.groups?.text
    // extracts '10'
    const num = elText.match(matcher)?.groups?.num
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Cypress 中的元素中提取文本,将其存储到变量中并对其进行操作 - How to extract text from an element in Cypress ,store it into variable and manipulate it JavaScript + E4X:如何测试元素是否具有文本节点? - JavaScript + E4X: how to test if an element has text nodes? 如何在没有webserver-stuff(即AJAX)的情况下获取外部JavaScript的文本 - How to get the text of a external JavaScript without webserver-stuff (i.e. AJAX) 从R中的第一个,第二个和第三个括号中提取文本 - Extract text from first, second and third brackets in R Python在关键字和方括号后提取文本 - Python extract text after keywords and brackets 在ByteString或Text上是否有单子/适用地图(即遍历/ mapM)功能? - Are there monadic/applicative map (i.e. traverse/mapM) functions over ByteString or Text? 使用JQuery提取元素中的文本 - Extract the Text in a Element with JQuery 从网站提取正文文本,例如仅提取文章标题和文本,而不是网站中的所有文本 - Body Text extraction from websites e.g. extract only article heading and text not all text in site 如何从文本中提取单个单词和网址? - How can I extract individual words and urls from a text? 如何从Perl中的纯文本中提取URL? - How can I extract URLs from plain text in Perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM