I am trying to count the number of full stops in a sentence. This code works fine when there is a full stop in the sentence. However, if there isn't a full stop i get an error message saying Cannot read property 'length' of null
What is the best way to write my code to handle this error, and display 0 as number of fullstops to the user.
See js fiddle here for the example
JS
$(".calculate").click(function() {
var input = $(".text-input").val();
var fullStopCount = 0;
fullStopCount = input.match(new RegExp("\\.", "g")).length;
$(".fullstop-count").text(". = " + fullStopCount);
});
HTML
<div class="textarea-holder">
<textarea class="text-input" name="textarea" rows="5" cols="30">This sentence contains one fullstop.</textarea>
</div>
<button class="calculate">Calculate</button>
<p class="fullstop-count"></p>
/\./ginstead ofnew RegExp("\\.", "g"), it's more readable, and may be faster.