Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript count words in string

var str = "your long string with many words.";
var wordCount = str.match(/(w+)/g).length;
alert(wordCount); //6

//    w+    between one and unlimited word characters
//    /g     greedy - don't stop after the first match
Comment

string methods javascript count number of words inside a string

<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>
Comment

PREVIOUS NEXT
Code Example
Javascript :: first duplicate javascript 
Javascript :: update angular materia; 
Javascript :: ajax datatable reload paging retained 
Javascript :: traverse an array in javascript 
Javascript :: string.find javascript 
Javascript :: jquery selector checked 
Javascript :: javascript remove empty object items 
Javascript :: javascript sample list 
Javascript :: falsy javascript 
Javascript :: javascript how to ceil number 
Javascript :: transitionduration 
Javascript :: how to change a string to number in javascript 
Javascript :: javascript if field exists 
Javascript :: javascript create array from 1 to n 
Javascript :: do you need a semicolon in javascript 
Javascript :: destructure dynamic property 
Javascript :: find the max length of string elements in an array 
Javascript :: how you can use javascript to play the sound for the button color selected 
Javascript :: Type io.invertase.firebase.BuildConfig is defined multiple times 
Javascript :: Capitalise a String 
Javascript :: export aab react native 
Javascript :: classlist.toggle 
Javascript :: async queue.push 
Javascript :: redux persist a non-serializable value was detected in an action in the path register 
Javascript :: how to send header in axios 
Javascript :: how could you implement javascript into java 
Javascript :: npx react-native run-ios --configuration Release device 
Javascript :: js how to get data fetch 
Javascript :: react scroll reset in component 
Javascript :: set timeout javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =