Two common methods for JS to determine the number of occurrences of a character in another string: js2
This document describes two common methods for determining the number of occurrences of a character in another string in JavaScript. We will share this with you for your reference. The details are as follows:
Two methods are provided for search verification.
1. Obtain the length principle by splitting
Var s = 'www .jb51.net'; var n = (s. split ('.'). length-1; alert (n); // pop up 2
2. Regular Expression
Function patch (re, s) {re = eval ("/" + re + "/ig") return s. match (re ). length;} var s = 'tools .jb51.net'; alert (patch ('T', s); // pop up 2
PS: For Regular Expression operations, here we provide two very convenient online tools for your reference:
JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript
Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg
In addition, we recommend two easy-to-use online character statistics tools for your reference:
Online word count statistics tools:
Http://tools.jb51.net/code/zishutongji
Online character statistics and editing tools:
Http://tools.jb51.net/code/char_tongji