4

I have this jsfiddle.

I have certain words which, when the user types them, it needs to be alerted to the user. Right now when I type a certain line, for example, I love ants it alerts me the full line I love ants, but I want only ants to be alerted to me. (In my example ants is a word need to be filtered. Refer to the 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());
   }       
});
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Standin.Wolf
  • 1,224
  • 1
  • 10
  • 32
  • I'm not quite understanding you.. *"it alerts me the full line I love ants but I want only ants to be alerted to me."* – Josh Crozier Mar 10 '14 at 04:57
  • Its alerting "ants" and "Words" – Pratik Joshi Mar 10 '14 at 04:57
  • possible duplicate of [JavaScript code to filter out common words in a string](http://stackoverflow.com/questions/6686718/javascript-code-to-filter-out-common-words-in-a-string) – 13ruce1337 Mar 10 '14 at 04:58
  • but it needs to alert once at a time,not the full line @PratikJoshi...then if i type something else,it goes on alerting me – Standin.Wolf Mar 10 '14 at 04:59
  • @JoshC i will give an example...if i type `hello world` it doesnt alert,which is proper.then I type `hello world ants` then it alerted me but instead of alerting just `ants` it alerts me `hello world ants` – Standin.Wolf Mar 10 '14 at 05:01

1 Answers1

2

You can change it to show just the matched part using this:

alert($(this).val().match(reg));

Here's a jsFiddle.

If you want a separate alert for each match you will have to do a loop.

EDIT: Here's a version that alerts once each time the user types a word in the list.

EDIT: And here's a version that demonstrates the optional use of regexes instead of words. Note that any slashes must be double-escaped. So, for example, to match ants but not pants, add \\bants\\b to the word list. As an example, this demo will match \bb..\b (so, bar, baz, etc., but not batters).

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • could u jsfiddle it for me if possible? – Standin.Wolf Mar 10 '14 at 05:03
  • I just added a fiddle for you. – elixenide Mar 10 '14 at 05:04
  • thnx for the answer..but everytime it alerts me once i type the word 'ants' which i dont want.only the first time i want that word to be alerted,i.e the word should be alerted once i type it ,thats it..not after that..is it possible like that? – Standin.Wolf Mar 10 '14 at 05:08
  • @JoshC no man..this is the same thing that i had..it is alerting the full sentence once i have typed the censored word..Ed Cottrell answer is close that I have right now...just that it goes alerting me the filtered word again and again after it is typed – Standin.Wolf Mar 10 '14 at 05:11
  • So, you want only one alert when it matches, then let the user keep typing, right? – elixenide Mar 10 '14 at 05:17
  • @User211 exactly which - Josh's version, or my question? – elixenide Mar 10 '14 at 05:18
  • 1
    @EdCottrell at you..exactly what u said i want – Standin.Wolf Mar 10 '14 at 05:19
  • @JoshC it alerts me properly but if i type the same word again or the other filtered word,it doesnt alert me.. – Standin.Wolf Mar 10 '14 at 05:19
  • 1
    @User211 Pretty sure this is what you want: http://jsfiddle.net/TexasBrawler/fqthJ/2/ – elixenide Mar 10 '14 at 05:35
  • 2
    @EdCottrell thats exactly what i wanted...just add the jsfiddle to ur answer above...so that i can accept it as an answer..thnx – Standin.Wolf Mar 10 '14 at 05:37
  • @User211 I didn't really help ;) I'm just sitting here trying to figure out and understand Ed's answer :) – Josh Crozier Mar 10 '14 at 05:48
  • 1
    @JoshC [I added some documentation to the fiddle](http://jsfiddle.net/TexasBrawler/fqthJ/7/). – elixenide Mar 10 '14 at 05:52
  • I don't have any experience with ckeditor, but it should work, as long as your script is hosted on the same domain as the page. It will be more complex because it uses iframes. For example, you can do something like this `$('iframe.cke_wysiwyg_frame').contents().find("body").html()` to access the contents of the demo at http://ckeditor.com/demo. You just have to revise the script accordingly to access this content. – elixenide Mar 10 '14 at 06:30
  • @EdCottrell hi again,just noticed,If i type plants it matches which is wrong..what should i do?I read about some word boundaries but couldnt use it in my code..if u could help? – Standin.Wolf Mar 12 '14 at 06:17
  • @User211 You can do that; you just have to use a regex in your word list, instead of a simple word. [**Here's a version**](http://jsfiddle.net/TexasBrawler/fqthJ/19/) that demonstrates the optional use of regexes instead of words. Note that any slashes must be double-escaped. So, for example, to match `ants` but not `plants`, add `\\bants\\b` to the word list. As an example, this demo will match `\bb..\b` (so, `bar`, `baz`, etc., but not `batters`). – elixenide Mar 12 '14 at 14:05
  • thnx alot for the help again..@EdCottrell – Standin.Wolf Mar 13 '14 at 04:17
  • 3
    @EdCottrell 1 final question...i have modified my code a bit now...I am taking the words from a text file using the $.get function.For example `ants,words`.The code I have works,but when i type `pants` it doesnt work(i.e it alerts it due to the presence of `ants` in `pants`).How do i put \\b for this?this is part of my code `var filter=new Array();$.get('1.txt', function(data) { filter= data.split(',');` rest of the code is same as yours..just that I want to add \\b,but i dont know where to put it.. – Standin.Wolf Mar 17 '14 at 04:41
  • +1 for alerting "due to the presence of `ants` in `pants`." :) When you do `filter= data.split(',');`, add this as the next line: `for(x in filter) filter[x] = "\\\\b"+filter[x]+"\\\\b";` That will add the word breaks to each term in your list. Alternatively, just put them in `1.txt` as `\\bants\\b,\\bwords\\b`, etc. – elixenide Mar 17 '14 at 04:50
  • @EdCottrell its adding but to the words(i alerted filter and it showed me `\\bants\\b,\\bwords\\b`) but it isnt alerting.. – Standin.Wolf Mar 17 '14 at 04:57
  • Can you post a fiddle of the latest code or link to a demo? – elixenide Mar 17 '14 at 04:58
  • @EdCottrell it doesnt work on jsfiddle coz it cant retrieve the file...if u using xamp or wamp u cud add a text file and try it...i am posting the code on jsfiddle http://jsfiddle.net/Mj4x8/3/ ..der is ckeditor also but for some reason it isnt loading...i guess u r interested in the script – Standin.Wolf Mar 17 '14 at 05:05
  • @EdCottrell it worked..in your line of code `for(x in filter) filter[x] = "\\\\b"+filter[x]+"\\\\b";` i removed the two `\\` from the 4 u gave(`\\\\b`) i guess it was a mistake by u..thnx aagain... – Standin.Wolf Mar 17 '14 at 05:08