Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js string includes count

/** Function that count occurrences of a substring in a string;
 * @param {String} string               The string
 * @param {String} subString            The sub string to search for
 * @param {Boolean} [allowOverlapping]  Optional. (Default:false)
 *
 * @author Vitim.us https://gist.github.com/victornpb/7736865
 * @see Unit Test https://jsfiddle.net/Victornpb/5axuh96u/
 * @see http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string/7924240#7924240
 */
function occurrences(string, subString, allowOverlapping) {

    string += "";
    subString += "";
    if (subString.length <= 0) return (string.length + 1);

    var n = 0,
        pos = 0,
        step = allowOverlapping ? 1 : subString.length;

    while (true) {
        pos = string.indexOf(subString, pos);
        if (pos >= 0) {
            ++n;
            pos += step;
        } else break;
    }
    return n;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: strong password javascript 
Javascript :: scirpt tag react 
Javascript :: javascript open method 
Javascript :: trigger keydown event javascript 
Javascript :: return the first matching object from an array 
Javascript :: empty javascript 
Javascript :: js some 
Javascript :: NodeJS Content-Type 
Javascript :: display component in popup angular 8 
Javascript :: location maps react native 
Javascript :: how to parse header in node.js lambda 
Javascript :: add a class in react 
Javascript :: js regrex 
Javascript :: is date 1 day ago javascript 
Javascript :: jquery accordion toggle close open 
Javascript :: javascript frames 
Javascript :: add object to another object javascript 
Javascript :: fs readfile promise 
Javascript :: Nodemailer Google Passport Oauth Strategy 
Javascript :: loopback 
Javascript :: Get the Timezone 
Javascript :: pluralize javascript 
Javascript :: 10 to the power of negative n 
Javascript :: js regular expression 
Javascript :: google analytics nextjs 
Javascript :: jquery modal popup 
Javascript :: Getting One Value from an Array of Items 
Javascript :: binarysearch 
Javascript :: Changes not staged for commit: modified: ../package.json 
Javascript :: push an item to array javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =