1

I have the following XML response which I converted to text:

let XMLText = '<?xml version="1.0" encoding="utf-8"?>  
<BlockList>  
  <CommittedBlocks>  
    <Block>  
      <Name>BlockId001</Name>  
      <Size>4194304</Size>  
    </Block>  
    <Block>  
      <Name>BlockId002</Name>  
      <Size>4194304</Size>  
    </Block>  
  </CommittedBlocks>  
</BlockList>'

now I am trying to find and count size: 4194304 from the text

I have tried using the search function but it only return the position of the string I am looking for

for example, following function returns 177, which is the position of the string

XMLText.search("4194304") 

0

1 Answer 1

2

Using regex, this is relatively easy to do.  Make your target string a capture group by putting it in parentheses and then count how many matches it has:

let numOfOccurrences = XMLText.match(/(4194304)/).length
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.