consider that i am getting a HTML format string and want to read the number of words & characters, Consider, i am getting,
var HTML = '<p>BODY Article By Archie(Used By Story Tool)</p>';
now i want to get number of words and characters
above html will look like:
BODY Article By Archie(Used By Story Tool)
IMPORTANT
- i want to avoid html tags while counting words or character
- avoid keywo开发者_StackOverflow中文版rds like
** **
etc.. - Ex. words and character should be counted of : (for current example) BODY Article By Archie(Used By Story Tool)
please help,
Thank You.To give an example for adamantium's suggestion:
var e = document.createElement("span");
e.innerHTML = '<p>BODY Article By Archie(Used By Story Tool)</p>';
var text = e.textContent || e.innerText;
var characterCount = text.length;
var wordCount = text.split(/[\s\.\(\),]+/).length;
Update: Added other word-stop characters
Use a hidden HTML element that can render text like span or p
Assign the string to the innerHTML of the hidden element.
Count the characters using length property of innerText/textContent.
To read the word count you can
Split the innerText/textContent using empty space
Get the length of the returned array.
Algorithm:
- Sweep through the entire html
- Perform regex replaces
- replace <.*> (regex for anyting tat stays withing <>)by nothing
- replace / / by nothing
- tip: can be done by replace function in javascript. hunt on w3schools.com
Now you have the clutter out!
then perform a simple word/character count
精彩评论