Find number of spaces in a string using JavaScript
Advertisements
To get total number of spaces in a JavaScript string, we can use regex to match spaces in a string and get the count of matched array.
console.log(("The quick brown fox jumps over the lazy dog".match(/ /g) || []).length); //8 console.log(("Poopcode".match(/ /g) || []).length); //0