Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 706009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:07:06+00:00 2026-05-14T04:07:06+00:00

In Javascript is there a function that returns the number of times that a

  • 0

In Javascript is there a function that returns the number of times that a given string occurs?
I need to return a numeric value that is equal to the number of times that a given string occurs within a particular string for instance:

var myString = "This is a test text"

If I had to search for ‘te‘ in the above string it would return 2.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:07:06+00:00Added an answer on May 14, 2026 at 4:07 am

    Very nearly: You can use String#match to do this:

    var count = "This is a test text".match(/te/g).length;
    

    That uses the regular expression /te/g (search for “te” literally, globally) and asks the string to return an array of matches. The array’s length is then the count.

    Naturally that creates an intermediary array, which may not be ideal if you have a large result set. If you don’t mind looping:

    function countMatches(str, re) {
        var counter;
    
        counter = 0;
        while (re.test(str)) {
            ++counter;
        }
        return counter;
    }
    
    var count = countMatches("This is a test text", /te/g);
    

    That uses RegExp#test to find matches without creating intermediary arrays. (Thanks to kennebec for the comment pointing out that my earlier use of RegExp#exec in the above created intermediary arrays unnecessarily!) Whether it’s more efficient will depend entirely on how many of these you expect to match, since the version creating the one big array will probably be optimized within the String#match call and so be faster at the expense of more (temporary) memory use — a large result set may bog down trying to allocate memory, but a small one is unlikely to.

    Edit Re your comment below, if you’re not looking for patterns and you don’t mind looping, you may want to do this instead:

    function countMatches(str, substr) {
        var index, counter, sublength;
    
        sublength = substr.length;
        counter = 0;
        for (index = str.indexOf(substr);
             index >= 0;
             index = str.indexOf(substr, index + sublength))
        {
            ++counter;
        }
        return counter;
    }
    
    var count = countMatches("This is a test text", "te");
    

    There’s no pre-baked non-RegExp way to do this that I know of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 359k
  • Answers 359k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Answering my own question here after forever; I found the… May 14, 2026 at 2:27 pm
  • Editorial Team
    Editorial Team added an answer The simplest way to invoke an external process in Groovy… May 14, 2026 at 2:27 pm
  • Editorial Team
    Editorial Team added an answer SendMessage(hand, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0)) The MAKELPARAM(FALSE, 0) is telling… May 14, 2026 at 2:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.