Skip to content

Instantly share code, notes, and snippets.

@cchang62
Last active March 1, 2018 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cchang62/38b98472c75511c0e4623d03786ff066 to your computer and use it in GitHub Desktop.
Save cchang62/38b98472c75511c0e4623d03786ff066 to your computer and use it in GitHub Desktop.
var words="Hi there and hello there. there here Welcome and hello there. hello abc";
var wordcnt = words.replace(/[^\w\s]/g, "").split(/\s+/).reduce(function(map, word){
map[word] = (map[word]||0)+1;
return map;
}, Object.create(null));
console.log(wordcnt)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var words="Hi there and hello there. there here Welcome and hello there. hello abc";
var wordcnt = words.replace(/[^\w\s]/g, "").split(/\s+/).reduce(function(map, word){
map[word] = (map[word]||0)+1;
return map;
}, Object.create(null));
console.log(wordcnt)
</script>
<script id="jsbin-source-javascript" type="text/javascript">var words="Hi there and hello there. there here Welcome and hello there. hello abc";
var wordcnt = words.replace(/[^\w\s]/g, "").split(/\s+/).reduce(function(map, word){
map[word] = (map[word]||0)+1;
return map;
}, Object.create(null));
console.log(wordcnt)</script></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment