Reputation: 71
I'm trying ti use match() in order to parse through some text and return the amount of times the text shows up. I'm doing this by using a global match and then calling length on the array that is created from the match. Instead, I'm just getting a array with a single element.
$('button').click(function () {
checkword = "/" + specialWord + "/g";
finalAnswer = userText.match(specialWord);
console.log(finalAnswer);
$('#answer').html(finalAnswer + '<br>' + finalAnswer.length);
return finalAnswer;
});
For example my search for 'is' in "this is" should return an array with a length of two, correct?
Fiddle: http://jsfiddle.net/
Upvotes: 0
Views: 48
Reputation: 70732
Use a RegExp constructor to do this and you need to change .match(specialWord)
to checkword
instead.
checkword = new RegExp(specialWord, "g");
finalAnswer = userText.match(checkword);
Upvotes: 2