JSFiddle - React, Tailwind, and code Playground

by Laurent Dufour

HTML

<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>

JavaScript

function countWords(str) {
  var matches = str.match(/[\w\d\’\'-]+/gi);
  return matches ? matches.length : 0;
}
var test1 = "Cela |est |cool ; |cela |est |bien";
var test2 = "C'|est| 'n'|importe |quoi' !";
var test3 = "4/|2|(1+|1)=|1 |et |4";
document.getElementById('test1').innerHTML = countWords(test1);
document.getElementById('test2').innerHTML = countWords(test2);
document.getElementById('test3').innerHTML = countWords(test3);