Javascript trying to count multiple spaces as one — in the textarea, but does not work. Why?

0 like 0 dislike
4 views
Hello. Why script is not working as I expect? Try to write a single character, then a lot of spaces and another character. Blanks will be counted ALL. And I need it to count only one.
https://jsfiddle.net/49pemc0x/
by | 4 views

2 Answers

0 like 0 dislike
Wrong regular expression define - quotation marks are not needed. And easier. it can be done, somehow so.
by
0 like 0 dislike
Yes, the quotes are not needed!
And a bunch of extra code )) That this is enough :
$('#text').on('input', function() { var string = $(this).val(); string = string.replace(/\\s+/g, ' ').trim(); $('#output').text(string.length); }); why two times to process a string?
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked May 22, 2019 by andrey_levushkin
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
33,932 users