
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to count a number of words in given string in JavaScript?
Using regular expressions it is easy to count number of words in a given string in javascript. There are some steps to be followed to count number of words
Steps to follow
We know that a sentence or a phrase is made up of words that are separated with spaces in between and there are some instances in which the words are separated by 2 or more spaces. A developer must notice all these points while calculating no of words.
Step-1
Exclude the start and end spaces of a string. The following line of regex expression will remove the start and end spaces of the given string.
str.replace(/(^\s*)|(\s*$)/gi,"");
Step-2
Try to reduce multiple spaces to a single space.
str.replace(/[ ]{2,}/gi," ");
Step-3
Try to exclude a new line with a start spacing.
str.replace(/
/,"
");
After performing all the above mention steps we will have a string with a single spaced words. On splitting the resulted string using split() method the words are going to join by a comma instead of spaces. Now using length() method we can get the resulted word count as shown in the following example.
Example
<html> <body> <script> function countWords(str) { str = str.replace(/(^\s*)|(\s*$)/gi,""); str = str.replace(/[ ]{2,}/gi," "); str = str.replace(/
/,"
"); return str.split(' ').length; } document.write(countWords(" Tutorix is one of the best E-learning platforms")); </script> </body> </html>
Output
8
- Related Articles
- How to count the number of words in a string in R?
- Count words in a given string in C++
- Java program to count words in a given string
- Python program to Count words in a given string?
- C# program to Count words in a given string
- Java Program to count the number of words in a String
- C# program to count the number of words in a string
- Finding the number of words in a string JavaScript
- How to count digits of given number? JavaScript
- How to count the number of occurrences of a character in a string in JavaScript?
- How to count the number of elements in an array below/above a given number (JavaScript)
- How to count the number of words in a text file using Java?
- Python program to count number of vowels using set in a given string
- How to count the occurrence of a specific string in a string in JavaScript
- Swapping adjacent words of a String in JavaScript
