4
How can I make a Javascript word filter? For example, I have a div
and within that div
do not want there to be certain words that are inside an array:
var badWords = ['teste', 'teste1', 'oteste2'];
How do I check if these words exist in the text and replace their content so that the first letter of that word appears, but the rest gets asterisks?
For example:
<div>Este texto tem as palavras: teste, teste1 e oteste2, e eu quero filtrar as palavras teste, oteste1 e teste2</div>
The above content would be replaced by something like this:
<div>Este texto tem as palavras: t****, t***** e o******, e eu quero filtrar as palavras t****, t***** e o******</div>
Remembering that I can’t use jQuery, it would need to be in pure Javascript precisely because it doesn’t justify using the full library just to do this...
What you tried so far?
– Beterraba
@Beet This is the problem I’m not experienced with Javascript, so everything I tried didn’t work. The most I could find was the index of these words, see: http://pastebin.com/5tvXRj1c All other tests failed... I tried. replace(), and others...
– waghcwb
Dude, these texts are generated like? in what language? Come from a database? I would not use javascript for this, if disabled in the browser, it was already your filter.
– Marcelo Aymone
The texts comes from a friendly common HTML page.. No databases.
– waghcwb
But why write HTML words that will be filtered? It wouldn’t be easier not to write what you can’t?
– Marcelo Aymone