0
Hi, everyone. Tell me, how to do .length not count spaces. Example, I want to write "1 2 3" that length was 3, not 5
4 Answers
+ 3
let str = "1 2 3 4 5"
let res = str.split(" ")
console.log(res.length)
+ 3
console.log('1 2 3'.split(' ').length);
+ 1
If you want to get rid of all whitespace, you can use this regular expression along with the replace method to replace whitespace with empty strings.
"1 2 3".replace(/\s/g, "").length
+ 1
there are many different ways to do it...
https://stackoverflow.com/questions/26389745/how-to-count-the-number-of-characters-without-spaces#:~:text=the%20code%20for%20the%20button,text(length)%3B