javascript - 如何在本机反应中将填充物放在我的输入字符串上?
问题描述
这是我想从输入字符串中过滤的单词
badwords: [{name: 'cake'}, {name: 'tea'}, {name: 'pastries'}]
我的搜索功能
searchUpdated() {
const badwords = [{name: 'cake'}, {name: 'tea'}, {name: 'pastries'}];
this.setState({sentence: this.state.review});
const sentence = this.state.review;
const words = sentence.split(' ');
for (let i = 0; i < words.length; i++) {
if (badwords.map((word) => word.name).includes(words[i])) {
alert('Sentence contains bard word: ' + words[i]);
}
}
}
我在这里设置评论值
<TextInput
style={[styles.Input, {color: this.state.txt ? 'red' : 'black'}]}
placeholder="Enter Review"
// autoCorrect={this.props.autoCorrect}
autoCapitalize={'none'}
returnKeyType={'done'}
keyboardType={'email-address'}
placeholderTextColor="gray"
value={this.state.review}
underlineColorAndroid="transparent"
onChangeText={(review) => {
this.setState({review});
}}
/>
我希望如果我写“我喜欢咖啡,但茶不好”,它会在屏幕上显示警告或其他内容,说明不要在字符串中的任何地方使用单词茶、蛋糕或糕点
解决方案
您可以通过将字符串拆分为单个单词来轻松完成此操作。然后遍历创建的数组并检查每个单词是否等于坏词之一。
这是一个如何完成此操作的简单示例:
const badwords = [{name: 'cake'}, {name: 'tea'}, {name: 'pastries'}];
const sentence = "I like coffee but tea was not good";
const words = sentence.split(' ');
for(let i = 0; i < words.length; i++){
if(badwords.map(word => word.name).includes(words[i])){
alert("Sentence contains a bard word: " + words[i]);
}
}
推荐阅读
- json - How to check in elasticsearch if a JSON object has a key using the DSL?
- amazon-web-services - AWS Dynamodb 扫描订购?
- java - 如何在一个活动中将弹出菜单添加到多个按钮?
- graphql - 根据 react-apollo 中的查询结果更新本地配置
- r - 如何计算一个变量的平均值对于不同变量的重复测量的特定值?
- windows - 在 CMD 批处理文件中,我可以确定它是否是从 powershell 运行的吗?
- java - 如何使用 Java 获取 JVM xmlxBytes 指标
- php - Laravel 测试 - UnauthorizedException:用户没有正确的角色
- sql - SSIS SQL statement with user parameter (int) in a like
- c++ - 为什么我无法理解的 C++ 代码中有不同的输出?