Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
How to find and count a specific word from text using JavaScript?

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") 

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!