Skip to content

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

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.