Javascript .match plus jQuery keyup(), double match and strange behaviour
Posted
by
Gremo
on Stack Overflow
See other posts from Stack Overflow
or by Gremo
Published on 2012-03-28T17:19:54Z
Indexed on
2012/03/28
17:30 UTC
Read the original article
Hit count: 232
Not really good in regular expression, but why when a match is found console.log
fires two times?
$('#name').keyup(function() {
var regex = /[\€]/g;
var count = (m = $(this).val().match(regex)) ? m.length : 0; // Num matches
console.log(count);
});
Output with 'hello':
0
0
0
0
0
After adding '€' symbol to 'hello' we have:
0
0
0
0
0
1
1
After adding 'h' symbol to 'hello€' we have:
0
0
0
0
0
1
1
1
Shouldn't be just one 1
after adding '€' to 'hello'?
© Stack Overflow or respective owner