js Method for Counting the Number of Specific Characters in String

  • 2021-07-07 06:16:05
  • OfStack

In this paper, an example is given to describe the method of counting the number of specific characters in string by js. Share it for your reference, as follows:


//js Count the number of specific characters contained in a string 
function getPlaceholderCount(strSource) {
  // The statistics string contains {} Or {xxXX} The number of 
  var thisCount = 0;
  strSource.replace(/\{[xX]+\}|\{\}/g, function (m, i) {
    //m For finding {xx} Element, i For index 
    thisCount++;
  });
  return thisCount;
}

PS: Here is another very convenient statistical tool:

Online word count tool:
http://tools.ofstack.com/code/zishutongji

For more readers interested in JavaScript related contents, please check the special topics of this site: "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: