hey, i try to write a code that users give the possibility to search words in a textarea. my problem is that the regex does not work.
as example: if i have "hello stackoverflow" in the textarea and the user search "hello" he recieve on 1 hit and this is correct, but if he search "hello lorem ipsum" he also recieve 1 hit.
regex = new RegExp($("input.search").val(), 'g');
counter = $("textarea").val().match(regex).len开发者_如何学Gogth;
what is wrong with the code?
regex = new RegExp($("input.search").val(), 'g');
counter = $("#textarea").val().match(regex).length
you are missing # in textarea, if you miss that you will get unspecified error
Works for me:
regex = new RegExp("hello", 'g')
"hello stackoverflow".match(regex).length // returns 1
regex = new RegExp("hello lorem ipso", 'g')
"hello stackoverflow".match(regex).length // match returns null
this worked for me:
http://jsfiddle.net/nLg2V/
精彩评论