gpt4 book ai didi

javascript - Vue js 过滤替换违禁词

转载 作者:行者123 更新时间:2023-11-28 04:23:29 25 4
gpt4 key购买 nike

在本例中,我们要过滤输入中的单词:

<input type="text" class="form-control" placeholder="Write something..." v-model="todoInput"">

这些是我们要从输入中替换的禁用词

"banned", "apple", "banana",

最佳答案

这是我们的 vue 实例。观察者将禁止的单词替换为星号(*)

const app = new Vue({
el: '#app',
data: function(){
return {
todoInput : '',
}
},
watch: {
todoInput: function(){
var banned = ["banned", "apple", "banana"]
for (var i = 0; i < banned.length; i++) {
if (this.todoInput.includes(banned[i])) {
this.todoInput = this.todoInput.replace(banned[i], "*".repeat(banned[i].length)) //sets the input to * times the length of the banned word
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>

<div id="app">

<input type="text" class="form-control"
placeholder="Write something..."
v-model="todoInput">

</div>

关于javascript - Vue js 过滤替换违禁词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45257882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com