JS Censoring algorithm
In javascript i have an array with words i would like to censor after putting some text in textarea, and when the word in it matches one in array, it is being replaced with "****". The problem is: for example the banned word is "word1", and when i trigger the event with clicking on "censor" button, the textarea.value, which is (for example) "word1!" is being replaced on "*****", but i want it to be "*****!".
There are no loops, cause i just tried to apply the algorithm for single banned word. Couldn't even do that. The 'start' variable is for further searching of other banned words within the textarea.value. Everything should be done with basic string/array methods, without using Regular Expressions.
let getId = x => document.getElementById(x);
let banned = ['word1','word2','word3'];
let start = 0;
getId('censor').addEventListener('click', function(){
let stars = '';
for(let i=0;i < banned[0].length; i++) {
stars+= '*';
}
let str = getId('text').value;
str = str.split(' ');
let found = str.indexOf(banned[0],start);
str.splice(found,1, stars);
str = str.join(' ');
getId('text').value = str;
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>javascript html arrays string
add a comment |
In javascript i have an array with words i would like to censor after putting some text in textarea, and when the word in it matches one in array, it is being replaced with "****". The problem is: for example the banned word is "word1", and when i trigger the event with clicking on "censor" button, the textarea.value, which is (for example) "word1!" is being replaced on "*****", but i want it to be "*****!".
There are no loops, cause i just tried to apply the algorithm for single banned word. Couldn't even do that. The 'start' variable is for further searching of other banned words within the textarea.value. Everything should be done with basic string/array methods, without using Regular Expressions.
let getId = x => document.getElementById(x);
let banned = ['word1','word2','word3'];
let start = 0;
getId('censor').addEventListener('click', function(){
let stars = '';
for(let i=0;i < banned[0].length; i++) {
stars+= '*';
}
let str = getId('text').value;
str = str.split(' ');
let found = str.indexOf(banned[0],start);
str.splice(found,1, stars);
str = str.join(' ');
getId('text').value = str;
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>javascript html arrays string
You could use replace to replace the exact word that was found with the stars and that would keep the rest of the word intact.
– apokryfos
Nov 20 '18 at 12:50
add a comment |
In javascript i have an array with words i would like to censor after putting some text in textarea, and when the word in it matches one in array, it is being replaced with "****". The problem is: for example the banned word is "word1", and when i trigger the event with clicking on "censor" button, the textarea.value, which is (for example) "word1!" is being replaced on "*****", but i want it to be "*****!".
There are no loops, cause i just tried to apply the algorithm for single banned word. Couldn't even do that. The 'start' variable is for further searching of other banned words within the textarea.value. Everything should be done with basic string/array methods, without using Regular Expressions.
let getId = x => document.getElementById(x);
let banned = ['word1','word2','word3'];
let start = 0;
getId('censor').addEventListener('click', function(){
let stars = '';
for(let i=0;i < banned[0].length; i++) {
stars+= '*';
}
let str = getId('text').value;
str = str.split(' ');
let found = str.indexOf(banned[0],start);
str.splice(found,1, stars);
str = str.join(' ');
getId('text').value = str;
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>javascript html arrays string
In javascript i have an array with words i would like to censor after putting some text in textarea, and when the word in it matches one in array, it is being replaced with "****". The problem is: for example the banned word is "word1", and when i trigger the event with clicking on "censor" button, the textarea.value, which is (for example) "word1!" is being replaced on "*****", but i want it to be "*****!".
There are no loops, cause i just tried to apply the algorithm for single banned word. Couldn't even do that. The 'start' variable is for further searching of other banned words within the textarea.value. Everything should be done with basic string/array methods, without using Regular Expressions.
let getId = x => document.getElementById(x);
let banned = ['word1','word2','word3'];
let start = 0;
getId('censor').addEventListener('click', function(){
let stars = '';
for(let i=0;i < banned[0].length; i++) {
stars+= '*';
}
let str = getId('text').value;
str = str.split(' ');
let found = str.indexOf(banned[0],start);
str.splice(found,1, stars);
str = str.join(' ');
getId('text').value = str;
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>let getId = x => document.getElementById(x);
let banned = ['word1','word2','word3'];
let start = 0;
getId('censor').addEventListener('click', function(){
let stars = '';
for(let i=0;i < banned[0].length; i++) {
stars+= '*';
}
let str = getId('text').value;
str = str.split(' ');
let found = str.indexOf(banned[0],start);
str.splice(found,1, stars);
str = str.join(' ');
getId('text').value = str;
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>let getId = x => document.getElementById(x);
let banned = ['word1','word2','word3'];
let start = 0;
getId('censor').addEventListener('click', function(){
let stars = '';
for(let i=0;i < banned[0].length; i++) {
stars+= '*';
}
let str = getId('text').value;
str = str.split(' ');
let found = str.indexOf(banned[0],start);
str.splice(found,1, stars);
str = str.join(' ');
getId('text').value = str;
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>javascript html arrays string
javascript html arrays string
edited Nov 20 '18 at 12:47
Luis felipe De jesus Munoz
4,42811031
4,42811031
asked Nov 20 '18 at 12:45
Bobby FadeBobby Fade
33
33
You could use replace to replace the exact word that was found with the stars and that would keep the rest of the word intact.
– apokryfos
Nov 20 '18 at 12:50
add a comment |
You could use replace to replace the exact word that was found with the stars and that would keep the rest of the word intact.
– apokryfos
Nov 20 '18 at 12:50
You could use replace to replace the exact word that was found with the stars and that would keep the rest of the word intact.
– apokryfos
Nov 20 '18 at 12:50
You could use replace to replace the exact word that was found with the stars and that would keep the rest of the word intact.
– apokryfos
Nov 20 '18 at 12:50
add a comment |
2 Answers
2
active
oldest
votes
let getId = x => document.getElementById(x);
let banned = ['word1', 'word2', 'word3'];
getId('add').addEventListener('click', function() {
banned.push(getId('banInput').value)
getId('banInput').value = ''
console.log(banned)
})
getId('censor').addEventListener('click', function() {
let str = getId('text').value;
let arr = str.split(/b/) // array of words
let censored = arr.map(word => banned.includes(word) ? //if word is in banned
'*'.repeat(word.length) // replace with *
:
word) // leave as is
getId('text').value = censored.join(''); // back to string
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>add a comment |
The easiest solution would be to use a regular expression to filter each word against a list of censored words.
Below i do this inside of a String.replace statement to simplify the process.
//Censor function
var censored = ;
function censor(input) {
return input.replace(/w+/igm, function (word) {
if (censored.some(function (cWord) { return cWord.toLowerCase() === word.toLowerCase(); })) {
return word.replace(/./igm, '*');
}
return word;
});
}
// HTML NODES
var inputField = document.body.appendChild(document.createElement("textarea"));
var outputField = document.body.appendChild(document.createElement("textarea"));
outputField.readOnly = true;
document.body.appendChild(document.createElement("br"));
var listInput = document.body.appendChild(document.createElement("input"));
listInput.title = listInput.placeholder = "insert censored words, seperate by " "";
listInput.value = '';
//Update functions
function updateOutput() {
outputField.value = censor(inputField.value);
}
function updateCensorList() {
censored = listInput.value.split(" ");
if (censored == null) {
censored = [''];
}
else {
censored = censored.map(function (a) { return a.trim(); });
}
updateOutput();
}
//Bind events
listInput.addEventListener("change", updateCensorList);
listInput.addEventListener("keyup", updateCensorList);
inputField.addEventListener("change", updateOutput);
inputField.addEventListener("keyup", updateOutput);add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393292%2fjs-censoring-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
let getId = x => document.getElementById(x);
let banned = ['word1', 'word2', 'word3'];
getId('add').addEventListener('click', function() {
banned.push(getId('banInput').value)
getId('banInput').value = ''
console.log(banned)
})
getId('censor').addEventListener('click', function() {
let str = getId('text').value;
let arr = str.split(/b/) // array of words
let censored = arr.map(word => banned.includes(word) ? //if word is in banned
'*'.repeat(word.length) // replace with *
:
word) // leave as is
getId('text').value = censored.join(''); // back to string
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>add a comment |
let getId = x => document.getElementById(x);
let banned = ['word1', 'word2', 'word3'];
getId('add').addEventListener('click', function() {
banned.push(getId('banInput').value)
getId('banInput').value = ''
console.log(banned)
})
getId('censor').addEventListener('click', function() {
let str = getId('text').value;
let arr = str.split(/b/) // array of words
let censored = arr.map(word => banned.includes(word) ? //if word is in banned
'*'.repeat(word.length) // replace with *
:
word) // leave as is
getId('text').value = censored.join(''); // back to string
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>add a comment |
let getId = x => document.getElementById(x);
let banned = ['word1', 'word2', 'word3'];
getId('add').addEventListener('click', function() {
banned.push(getId('banInput').value)
getId('banInput').value = ''
console.log(banned)
})
getId('censor').addEventListener('click', function() {
let str = getId('text').value;
let arr = str.split(/b/) // array of words
let censored = arr.map(word => banned.includes(word) ? //if word is in banned
'*'.repeat(word.length) // replace with *
:
word) // leave as is
getId('text').value = censored.join(''); // back to string
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>let getId = x => document.getElementById(x);
let banned = ['word1', 'word2', 'word3'];
getId('add').addEventListener('click', function() {
banned.push(getId('banInput').value)
getId('banInput').value = ''
console.log(banned)
})
getId('censor').addEventListener('click', function() {
let str = getId('text').value;
let arr = str.split(/b/) // array of words
let censored = arr.map(word => banned.includes(word) ? //if word is in banned
'*'.repeat(word.length) // replace with *
:
word) // leave as is
getId('text').value = censored.join(''); // back to string
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>let getId = x => document.getElementById(x);
let banned = ['word1', 'word2', 'word3'];
getId('add').addEventListener('click', function() {
banned.push(getId('banInput').value)
getId('banInput').value = ''
console.log(banned)
})
getId('censor').addEventListener('click', function() {
let str = getId('text').value;
let arr = str.split(/b/) // array of words
let censored = arr.map(word => banned.includes(word) ? //if word is in banned
'*'.repeat(word.length) // replace with *
:
word) // leave as is
getId('text').value = censored.join(''); // back to string
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>let getId = x => document.getElementById(x);
let banned = ['word1', 'word2', 'word3'];
getId('add').addEventListener('click', function() {
banned.push(getId('banInput').value)
getId('banInput').value = ''
console.log(banned)
})
getId('censor').addEventListener('click', function() {
let str = getId('text').value;
let arr = str.split(/b/) // array of words
let censored = arr.map(word => banned.includes(word) ? //if word is in banned
'*'.repeat(word.length) // replace with *
:
word) // leave as is
getId('text').value = censored.join(''); // back to string
})<div class="container">
<form action="">
<input type="text" id="banInput">
<input type="button" value="Add word" id="add">
</form>
<textarea name="" id="text"></textarea>
<input type="button" value="censor" id="censor">
</div>edited Nov 20 '18 at 13:14
answered Nov 20 '18 at 13:07
Smollet777Smollet777
1,36011015
1,36011015
add a comment |
add a comment |
The easiest solution would be to use a regular expression to filter each word against a list of censored words.
Below i do this inside of a String.replace statement to simplify the process.
//Censor function
var censored = ;
function censor(input) {
return input.replace(/w+/igm, function (word) {
if (censored.some(function (cWord) { return cWord.toLowerCase() === word.toLowerCase(); })) {
return word.replace(/./igm, '*');
}
return word;
});
}
// HTML NODES
var inputField = document.body.appendChild(document.createElement("textarea"));
var outputField = document.body.appendChild(document.createElement("textarea"));
outputField.readOnly = true;
document.body.appendChild(document.createElement("br"));
var listInput = document.body.appendChild(document.createElement("input"));
listInput.title = listInput.placeholder = "insert censored words, seperate by " "";
listInput.value = '';
//Update functions
function updateOutput() {
outputField.value = censor(inputField.value);
}
function updateCensorList() {
censored = listInput.value.split(" ");
if (censored == null) {
censored = [''];
}
else {
censored = censored.map(function (a) { return a.trim(); });
}
updateOutput();
}
//Bind events
listInput.addEventListener("change", updateCensorList);
listInput.addEventListener("keyup", updateCensorList);
inputField.addEventListener("change", updateOutput);
inputField.addEventListener("keyup", updateOutput);add a comment |
The easiest solution would be to use a regular expression to filter each word against a list of censored words.
Below i do this inside of a String.replace statement to simplify the process.
//Censor function
var censored = ;
function censor(input) {
return input.replace(/w+/igm, function (word) {
if (censored.some(function (cWord) { return cWord.toLowerCase() === word.toLowerCase(); })) {
return word.replace(/./igm, '*');
}
return word;
});
}
// HTML NODES
var inputField = document.body.appendChild(document.createElement("textarea"));
var outputField = document.body.appendChild(document.createElement("textarea"));
outputField.readOnly = true;
document.body.appendChild(document.createElement("br"));
var listInput = document.body.appendChild(document.createElement("input"));
listInput.title = listInput.placeholder = "insert censored words, seperate by " "";
listInput.value = '';
//Update functions
function updateOutput() {
outputField.value = censor(inputField.value);
}
function updateCensorList() {
censored = listInput.value.split(" ");
if (censored == null) {
censored = [''];
}
else {
censored = censored.map(function (a) { return a.trim(); });
}
updateOutput();
}
//Bind events
listInput.addEventListener("change", updateCensorList);
listInput.addEventListener("keyup", updateCensorList);
inputField.addEventListener("change", updateOutput);
inputField.addEventListener("keyup", updateOutput);add a comment |
The easiest solution would be to use a regular expression to filter each word against a list of censored words.
Below i do this inside of a String.replace statement to simplify the process.
//Censor function
var censored = ;
function censor(input) {
return input.replace(/w+/igm, function (word) {
if (censored.some(function (cWord) { return cWord.toLowerCase() === word.toLowerCase(); })) {
return word.replace(/./igm, '*');
}
return word;
});
}
// HTML NODES
var inputField = document.body.appendChild(document.createElement("textarea"));
var outputField = document.body.appendChild(document.createElement("textarea"));
outputField.readOnly = true;
document.body.appendChild(document.createElement("br"));
var listInput = document.body.appendChild(document.createElement("input"));
listInput.title = listInput.placeholder = "insert censored words, seperate by " "";
listInput.value = '';
//Update functions
function updateOutput() {
outputField.value = censor(inputField.value);
}
function updateCensorList() {
censored = listInput.value.split(" ");
if (censored == null) {
censored = [''];
}
else {
censored = censored.map(function (a) { return a.trim(); });
}
updateOutput();
}
//Bind events
listInput.addEventListener("change", updateCensorList);
listInput.addEventListener("keyup", updateCensorList);
inputField.addEventListener("change", updateOutput);
inputField.addEventListener("keyup", updateOutput);The easiest solution would be to use a regular expression to filter each word against a list of censored words.
Below i do this inside of a String.replace statement to simplify the process.
//Censor function
var censored = ;
function censor(input) {
return input.replace(/w+/igm, function (word) {
if (censored.some(function (cWord) { return cWord.toLowerCase() === word.toLowerCase(); })) {
return word.replace(/./igm, '*');
}
return word;
});
}
// HTML NODES
var inputField = document.body.appendChild(document.createElement("textarea"));
var outputField = document.body.appendChild(document.createElement("textarea"));
outputField.readOnly = true;
document.body.appendChild(document.createElement("br"));
var listInput = document.body.appendChild(document.createElement("input"));
listInput.title = listInput.placeholder = "insert censored words, seperate by " "";
listInput.value = '';
//Update functions
function updateOutput() {
outputField.value = censor(inputField.value);
}
function updateCensorList() {
censored = listInput.value.split(" ");
if (censored == null) {
censored = [''];
}
else {
censored = censored.map(function (a) { return a.trim(); });
}
updateOutput();
}
//Bind events
listInput.addEventListener("change", updateCensorList);
listInput.addEventListener("keyup", updateCensorList);
inputField.addEventListener("change", updateOutput);
inputField.addEventListener("keyup", updateOutput);//Censor function
var censored = ;
function censor(input) {
return input.replace(/w+/igm, function (word) {
if (censored.some(function (cWord) { return cWord.toLowerCase() === word.toLowerCase(); })) {
return word.replace(/./igm, '*');
}
return word;
});
}
// HTML NODES
var inputField = document.body.appendChild(document.createElement("textarea"));
var outputField = document.body.appendChild(document.createElement("textarea"));
outputField.readOnly = true;
document.body.appendChild(document.createElement("br"));
var listInput = document.body.appendChild(document.createElement("input"));
listInput.title = listInput.placeholder = "insert censored words, seperate by " "";
listInput.value = '';
//Update functions
function updateOutput() {
outputField.value = censor(inputField.value);
}
function updateCensorList() {
censored = listInput.value.split(" ");
if (censored == null) {
censored = [''];
}
else {
censored = censored.map(function (a) { return a.trim(); });
}
updateOutput();
}
//Bind events
listInput.addEventListener("change", updateCensorList);
listInput.addEventListener("keyup", updateCensorList);
inputField.addEventListener("change", updateOutput);
inputField.addEventListener("keyup", updateOutput);//Censor function
var censored = ;
function censor(input) {
return input.replace(/w+/igm, function (word) {
if (censored.some(function (cWord) { return cWord.toLowerCase() === word.toLowerCase(); })) {
return word.replace(/./igm, '*');
}
return word;
});
}
// HTML NODES
var inputField = document.body.appendChild(document.createElement("textarea"));
var outputField = document.body.appendChild(document.createElement("textarea"));
outputField.readOnly = true;
document.body.appendChild(document.createElement("br"));
var listInput = document.body.appendChild(document.createElement("input"));
listInput.title = listInput.placeholder = "insert censored words, seperate by " "";
listInput.value = '';
//Update functions
function updateOutput() {
outputField.value = censor(inputField.value);
}
function updateCensorList() {
censored = listInput.value.split(" ");
if (censored == null) {
censored = [''];
}
else {
censored = censored.map(function (a) { return a.trim(); });
}
updateOutput();
}
//Bind events
listInput.addEventListener("change", updateCensorList);
listInput.addEventListener("keyup", updateCensorList);
inputField.addEventListener("change", updateOutput);
inputField.addEventListener("keyup", updateOutput);edited Nov 20 '18 at 13:30
answered Nov 20 '18 at 13:04
Emil S. JørgensenEmil S. Jørgensen
4,6491617
4,6491617
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393292%2fjs-censoring-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You could use replace to replace the exact word that was found with the stars and that would keep the rest of the word intact.
– apokryfos
Nov 20 '18 at 12:50