Advertisement
dimipan80

Substring Count

Nov 12th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function countSubstringOccur(arr) that accepts as parameter an array of 2 elements
  2.  arr [keyword, text]. The function finds how many times a substring is contained in a given text
  3.  (perform case insensitive search). Write JS program substringSearch.js that invokes your function
  4.  with the sample input data below and prints the output at the console. */
  5.  
  6. "use strict";
  7.  
  8. function countSubstringOccur(arr) {
  9.     var text = arr[1].toLowerCase();
  10.     var pattern = new RegExp(arr[0], 'g');
  11.     var counterOccurs = (text.match(pattern) || []).length;
  12.  
  13.     return counterOccurs;
  14. }
  15.  
  16. console.log(countSubstringOccur(['in', "We are living in a yellow submarine. We don't have anything else." +
  17. "Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days."]));
  18.  
  19. console.log(countSubstringOccur(['your', 'No one heard a single word you said.' +
  20. 'They should have seen it in your eyes. What was going around your head.']));
  21.  
  22. console.log(countSubstringOccur(['but', "But you were living in another world tryin'" +
  23. "to get your message through."]));
  24.  
  25. console.log(countSubstringOccur(['hi', "Hidden networks say 'Hi' only to Hitachi devices. Hi, said Matuhi. HI!"]));
  26.  
  27. console.log(countSubstringOccur(['SoftUni', "The Software University (SoftUni) trains software engineers, gives them" +
  28. "a profession and a job. Visit us at http://softuni.bg. Enjoy the SoftUnification at SoftUni.BG." +
  29. "Contact us.Email: INFO@SOFTUNI.BG. Facebook: https://www.facebook.com/SoftwareUniversity." +
  30. "YouTube: http://www.youtube.com/SoftwareUniversity. Google+: https://plus.google.com/+SoftuniBg/." +
  31. "Twitter: https://twitter.com/softunibg. GitHub: https://github.com/softuni"]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement