javascript - Alert filtered words using Jquery -
i have this jsfiddle.
i have words which, when user types them, needs alerted user. right when type line, example, i love ants
alerts me full line i love ants
, want ants
alerted me. (in example ants word need filtered. refer jsfiddle.)
var filter = ['ants', 'words'], reg = new regexp("(" + filter.join("|") + ")", "g"); $('#texta').keyup(function(){ $("#dest").html( $(this).val().replace(reg, "<mark>$1</mark>") ); if(reg.test($(this).val())==true) { alert($(this).val()); } });
you can change show matched part using this:
alert($(this).val().match(reg));
here's jsfiddle.
if want separate alert each match have loop.
edit: here's version alerts once each time user types word in list.
edit: , here's version demonstrates optional use of regexes instead of words. note slashes must double-escaped. so, example, match ants
not pants
, add \\bants\\b
word list. example, demo match \bb..\b
(so, bar
, baz
, etc., not batters
).
Comments
Post a Comment