我是Javascript的新手,所以请耐心等待。这段代码的目的只是在表单提交时阻止textarea中的“废话”,“丑陋”和“brat”。我想要它,以便在用户按下提交后,坏词会出现( * *)。这纯粹是我被分配的练习课,所以它不需要有任何实际用途。
此代码的问题在于,一旦按下提交,textarea中的所有文本都将消失。因此,没有任何词语可以阻止。
以下是代码:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Bad Words Blocker Test</title>
<script type="text/javascript" language="javascript">
var buttonPress = function ()
{
var com = getElementById(comments);
var filterWords = ["crap", "ugly", "brat"];
// "i" is to ignore case and "g" for global
var rgx = new RegExp(filterWords.join(""), "gi");
function WordFilter(str) {
return str.replace(rgx, "****");
}
}
</script>
</head>
<body>
<form name="badwords" method="post" action="">
<textarea name="comments" id="comments" rows="5" cols="50"></textarea>
<br />
<input id="formSub" type="submit" onclick="(buttonPress())" value="Submit!" />
</form>
</body>
</html>
答案 0 :(得分:4)
将操作细分为功能是一种很好的做法。
var button = document.getElementById('formSub');
function replaceWords(event) {
//Prevent form submission to server
event.preventDefault();
var commentContent = document.getElementById('comments');
var badWords = ["crap", "ugly", "brat", "basterddouch"];
var censored = censore(commentContent.value, badWords);
commentContent.value = censored;
}
function censore(string, filters) {
// "i" is to ignore case and "g" for global "|" for OR match
var regex = new RegExp(filters.join("|"), "gi");
return string.replace(regex, function (match) {
//replace each letter with a star
var stars = '';
for (var i = 0; i < match.length; i++) {
stars += '*';
}
return stars;
});
}
button.addEventListener('click', replaceWords);
答案 1 :(得分:2)
var my = "You son of a bitch.You are fool . www.google.com";
var badWord = /crap|ugly|brat|fool|fuck|fucking|f\*cking|f\*ck|bitch|b\*tch|shit|sh\*t|fool|dumb|couch potato|arse|arsehole|asshole|\*ssh\*l\*|\*\*\*\*|c\*ck|\*\*\*\*sucker|c\*cks\*ck\*r|\*\*\*\*|c\*nt|dickhead|d\*c\*h\*a\*|\*\*\*\*|f\*c\*|\*\*\*\*wit|f\*ckw\*t|fuk|f\*k|fuking|f\*k\*ng|mother\*\*\*\*er|m\*th\*rf\*ck\*r|\*\*\*\*\*\*|n\*gg\*r|pussy|p\*ssy|\*\*\*\*|sh\*t|wanker|w\*nk\*r|wankers|w\*nk\*rs|whore|wh\*r\*|slag| sl\*g|\*\*\*\*\*|b\*tch|f u c k|f\*c\*|b.i.t.c.h|b\*tch|d-i-c-k|d\*\*\*/gi;
my = my.replace(badWord,"****");
alert(my);
在java脚本块中使用上面的代码。对于更多基于java脚本的正则表达式。查看regexp库https://github.com/javaquery/regexp
编辑:
在正则表达式中添加坏词。其次是|
(或)
答案 2 :(得分:0)
你应该调用WordFilter()
函数
你也应该抓住onSubmit事件的形式,而不是点击按钮
你以错误的方式致电getElementById
并且正则表达式不正确:)
所以代码应该是这样的(它可以工作,我已经测试过了):
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Bad Words Blocker Test</title>
<script type="text/javascript" language="javascript">
var buttonPress = function ()
{
var com = document.getElementById('comments');
var filterWords = ["crap", "ugly", "brat"];
// "i" is to ignore case and "g" for global
var rgx = new RegExp("("+filterWords.join("|")+")", "gi");
com.value = com.value.replace(rgx, "****");
// change this to 'return true;' when you will be sure that all your bad words are catched and the form is ready to be submitted
return false;
}
</script>
</head>
<body>
<form name="badwords" method="post" action="" onsubmit="return buttonPress();">
<textarea name="comments" id="comments" rows="5" cols="50"></textarea>
<br />
<input id="formSub" type="submit" value="Submit!" />
</form>
</body>
</html>