{"id":3838,"date":"2013-05-14T19:05:46","date_gmt":"2013-05-14T19:05:46","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3838"},"modified":"2013-05-14T19:05:46","modified_gmt":"2013-05-14T19:05:46","slug":"counting-occurrences-of-word-in-string","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/counting-occurrences-of-word-in-string\/","title":{"rendered":"JavaScript: Count Occurrences Of String In String Examples"},"content":{"rendered":"<p>At some point or the other, it might be useful to count the number of occurrences of a specific word or a string in another string using JavaScript. In this article, I am going to share very easy example to do just that with the help of easy to follow examples. Read on to find out more.<\/p>\n<p><!--more--><\/p>\n<h2>How To Count\u00a0Occurrences In String Using JavaScript<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3838\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s look at how to do this with an example. Consider that we have a string that says: <strong>&#8220;This is a string.&#8221;<\/strong>. Now we want to find out the number of times the word <strong>&#8220;is&#8221;<\/strong> has repeated in that string. So here&#8217;s how it can be done in the most simplest way:<\/p>\n<h3>Example:<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;p&gt;The string that needs to be searched in is: &lt;strong&gt;\"This is a string.\"&lt;\/strong&gt;. So this is our Haystack.&lt;\/p&gt;\n&lt;p&gt;We want to search fo the word &lt;strong&gt;\"is\"&lt;\/strong&gt;. So this is our Needle.&lt;\/p&gt;\n&lt;p&gt;You should see an alert as soon as the page loads.&lt;\/p&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\nvar temp = \"This is a string.\";\n\/\/ the g in the regular expression says to search the whole string \n\/\/ rather than just find the first occurrence\nvar count = temp.match(\/is\/g);  \nalert('The word \"is\" is repeated ' + count.length + ' times.');\n&lt;\/script&gt;<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=3838\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>Simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to count the number of times a string or a word is repeated in another string using JavaScript? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point or the other, it might be useful to count the number of occurrences of a specific word or a string in another string using JavaScript. In this article, I am going to share very easy example to do just that with the help of easy to follow examples. Read on to find [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[42,43,41],"class_list":["post-3838","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-demos-2","tag-jquery-snippets","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-ZU","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=3838"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3838\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=3838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}