var index = "This is a string".indexOf("is");console.log(index);var length = "This is a string".match(/[a-z]/g).length;// where [a-z] is a regex wildcard expression thats why its slowerconsole.log(length);
var temp = "This is a string.";
function countOcurrences(str, value) {var regExp = new RegExp(value, "gi");return (str.match(regExp) || []).length;}
console.log(countOcurrences(temp, 'is'));
var myString = "This is a string.";var foundAtPosition = 0;var Count = 0;while (foundAtPosition != -1){foundAtPosition = myString.indexOf("is",foundAtPosition);if (foundAtPosition != -1){Count++;foundAtPosition++;}}document.write("There are " + Count + " occurrences of the word IS");
function keywordOccurrences(string, subString, allowOverlapping, caseInsensitive, wholeWord){
string += "";subString += "";if (subString.length <= 0) return (string.length + 1); //deal with empty strings
if(caseInsensitive){string = string.toLowerCase();subString = subString.toLowerCase();}
var n = 0,pos = 0,step = allowOverlapping ? 1 : subString.length,stringLength = string.length,subStringLength = subString.length;
while (true){pos = string.indexOf(subString, pos);if (pos >= 0){var matchPos = pos;pos += step; //slide forward the position pointer no matter what
if(wholeWord) //only whole word matches are desired{if(matchPos > 0) //if the string is not at the very beginning we need to check if the previous character is whitespace{if(!/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&\(\)*+,\-.\/:;<=>?@\[\]^_`{|}~]/.test(string[matchPos - 1])) //ignore punctuation{continue; //then this is not a match}}
var matchEnd = matchPos + subStringLength;if(matchEnd < stringLength - 1){if (!/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&\(\)*+,\-.\/:;<=>?@\[\]^_`{|}~]/.test(string[matchEnd])) //ignore punctuation{continue; //then this is not a match}}}
++n;} else break;}return n;}
var search_value = "This is a dummy sentence!";var letter = 'a'; /*Can take any letter, have put in a var if anyone wants to use this variable dynamically*/letter = letter && "string" === typeof letter ? letter : "";var count;for (var i = count = 0; i < search_value.length; count += (search_value[i++] == letter));console.log(count);
let str = 'As sly as a fox, as strong as an ox';
let target = 'as'; // let's look for it
let pos = 0;while (true) {let foundPos = str.indexOf(target, pos);if (foundPos == -1) break;
alert( `Found at ${foundPos}` );pos = foundPos + 1; // continue the search from the next position}
同样的算法可以更短:
let str = "As sly as a fox, as strong as an ox";let target = "as";
let pos = -1;while ((pos = str.indexOf(target, pos + 1)) != -1) {alert( pos );}
var str = 'stackoverflow';var arr = Array.from(str);console.log(arr);
for (let a = 0; a <= arr.length; a++) {var temp = arr[a];var c = 0;for (let b = 0; b <= arr.length; b++) {if (temp === arr[b]) {c++;}
}console.log(`the ${arr[a]} is counted for ${c}`)}
function checkOccurences(string, word) {return string.split(word).length - 1;}const text="Let us see. see above, see below, see forward, see backward, see left, see right until we will be right";const count=countOccurences(text,"see "); // 2
var mystring = 'This is the lorel ipsum text';var mycharArray = mystring.split('');var opArr = [];for(let i=0;i<mycharArray.length;i++){if(mycharArray[i]=='i'){//match the character you want to matchopArr.push(i);}}console.log(opArr); // it will return matching index positionconsole.log(opArr.length); // it will return length
string = "Xanthous: A person with yellow hair. Her hair was very xanthous in colour."count = string.split('').map((e,i) => { if(e === 'e') return i;}).filter(Boolean).length