JavaScript Check a string contain how many word in javascript check string contains,check string contain php,check string contain

I would like to check how many words in a string

eg.

 asdsd sdsds sdds 

3 words

The problem is , if there is more than one space between two substring , the result is not correct

here is my program

function trim(s) {   
  return s.replace(/^\s*|\s*$/g,"")   
} 

var str = trim(x[0].value);
var parts = str .split(" ");
alert (parts.length);

How to fix the problem? thanks for help

Answer:1

Please use this one Its already used by me in my project :

function countWords(){
    s = document.getElementById("inputString").value;
    s = s.replace(/(^\s*)|(\s*$)/gi,"");
    s = s.replace(/[ ]{2,}/gi," ");
    s = s.replace(/\n /,"\n");
    document.getElementById("wordcount").value = s.split(' ').length;
}
Answer:2
var parts = str .split(" ");
parts = parts.filter(function(elem, pos, self) {
     return elem !== "";
});

Please try this. Make sure you are using latest browser to use this code.

Answer:3

You could just use match with word boundaries:

var words = str.match(/\b\w+\b/g);

http://jsbin.com/abeluf/1/edit

Answer:4

It's easy to find out by using split method, but you need to sort out if there are any special characters. If you don't have an special characters in your string, then last line is enough to work.

s = document.getElementById("inputString").value;
s = s.replace(/(^\s*)|(\s*$)/gi,"");
s = s.replace(/[ ]{2,}/gi," ");
s = s.replace(/\n /,"\n");
document.getElementById("wordcount").value = s.split(' ').length;
Answer:5

try:

function trim(s) {   
  return s.replace(/^\s*|\s*$/g,"")   
} 
var regex = /\s+/gi;
var value = "this ss ";
var wordCount = value.trim().replace(regex, ' ').split(' ').length;
console.log( wordCount );
Answer:6

Below shown is my HTMLcode : <!doctype html> <html lang="en"> <title></title> <head> <script scr ="myprog.js"> </script> </head> <...

Below shown is my HTMLcode : <!doctype html> <html lang="en"> <title></title> <head> <script scr ="myprog.js"> </script> </head> <...

I am loading all website's pages into the main index page, and updating URL display by splitting the href into segments and adding the segments after the main domain name with .hash function, like ...

I am loading all website's pages into the main index page, and updating URL display by splitting the href into segments and adding the segments after the main domain name with .hash function, like ...

my function is as easy as this: function changeColor(color, opacity, obj) { var colors = obj.getElementsByClassName("color_elem"); for (var i = 0; i < colors.length; i++) { ...

my function is as easy as this: function changeColor(color, opacity, obj) { var colors = obj.getElementsByClassName("color_elem"); for (var i = 0; i < colors.length; i++) { ...

I have function in my code, which suppose to gradually decrease the refreshment of the page from 50 sec to 10 if the button is pressed. Here it is: <div id="signin" onclick="pgeReload()"></...

I have function in my code, which suppose to gradually decrease the refreshment of the page from 50 sec to 10 if the button is pressed. Here it is: <div id="signin" onclick="pgeReload()"></...

  1. retain value variable
  2. retain variable value in sas
  3. retain variable value in vba
  4. retain variable value in javascript
  5. retain variable value c#
  6. retain javascript variable value after postback
  7. retain value in global variable
  8. excel vba retain variable value
  9. python retain value of variable
  10. how to retain variable value in java