We would like to know how to find number of spaces in a string.
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var x = "abc def rr tt".match(/\s/g).length;
document.writeln(x);
x = "34 45n v".match(/\s/g).length;
document.writeln(x);
<!-- www .j a v a2s. c o m-->
document.writeln('<br/>');
var str1 = "abc def rr tt"; // should return 3
var str2 = "34 45n v"; // should return 2
document.writeln(str2.split(' ').length-1)
</script>
</body>
</html>
The code above is rendered as follows: