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  Reverse Linked List - Leetcode Challenge - Python Solution

Leave a Reply

Your email address will not be published.

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