Answers for "js count of all special characters"

9

how to check for special characters in javascript

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;

if(format.test(string)){
  return true;
} else {
  return false;
}
Posted by: Guest on October-04-2020
0

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);
Posted by: Guest on February-01-2022

Code answers related to "js count of all special characters"

Code answers related to "Javascript"

Browse Popular Code Answers by Language