I have the following Javascript which is part of a larger function. I am doing a javascript match then trying to get the length of the match. The problem is, the length always returns 1 even when it is 10 characters long.
var content = $(this).text();
var word = /#(\w+)/ig;
var query = content.match(word);
alert(query + ' | ' + query.length);
How can I get the accurate value for the length of the match?
Because the result of
String.matchwith a global regex is an array.query.lengthtells you there is only one match; you can access the matched string withquery[0].It appears to be a string in your
alertcall becauseArray.toStringis equivalent toarray.join(','). With just one element, it shows that single element.