Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

occurences of special character in a string javascript

// program to check the occurrence of a character

function countString(str, letter) {

    // creating regex 
    const re = new RegExp(letter, 'g');

    // matching the pattern
    const count = str.match(re).length;

    return count;
}

// take input from the user
const string = prompt('Enter a string: ');
const letterToCheck = prompt('Enter a letter to check: ');

//passing parameters and calling the function
const result = countString(string, letterToCheck);

// displaying the result
console.log(result);
Comment

PREVIOUS NEXT
Code Example
Javascript :: Mapping an Array to Elements with v-for 
Javascript :: how to replace import with require 
Javascript :: Uncaught TypeError: table.fnColReorder.transpose is not a function 
Javascript :: vscode decrease window tab 
Javascript :: jQuery form upload 
Javascript :: window coordinates 
Javascript :: save new 
Javascript :: push code from vscode using CL 
Javascript :: object empty or undefined 
Javascript :: js how to find not unic values in array 
Javascript :: array destructuring in js 
Javascript :: hello worled anglular script 
Javascript :: New year chaos solution 
Javascript :: show more vs editor shortcut key 
Javascript :: array.includes is not a function react 
Javascript :: submit file js 
Javascript :: know if a gridview is empty from javascript 
Javascript :: SHOPIFY CUSTOMER WITHOUT REGISTRATION 
Javascript :: Fix the transition judder at 0/60 seconds javascript30 js clock 
Javascript :: H.C.F Calculation Program 
Javascript :: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace 
Javascript :: send data to javscript 
Javascript :: Variable As Parameter In Self Invoking Function 
Javascript :: Return Instance Of Object 
Javascript :: angular reuse component with different data 
Javascript :: Will yield function Person 
Javascript :: How to Add Main Module API to Renderer process 
Javascript :: unreachable code detected javascript 
Javascript :: react native scan network 
Javascript :: JS function examples 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =