Skip to content

Find number of spaces in a string using JavaScript


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); 


console.log(("Poopcode".match(/ /g) || []).length);

See also  Most Common Word - Leetcode Challenge - Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.