javascript 敏感词过滤题 Cross the greatwall,We can reach every corner of the world.
2017-06-18 23:17
369 查看
今天做了一道题,现将思路及代码与大家分享。
题目如下:
题目描述:
大部分论坛、网站等,为了方便管理,都进行了关于敏感词的设定。在多数网站,敏感词一般是指带有敏感政治倾向、暴力倾向、不健康色彩的词或不文明语,也有一些网站根据自身实际情况,设定一些只适用于本网站的特殊敏感词。比如,当你发帖的时候带有某些事先设定的词时,这个贴是不能发出的。或者这个词被自动替换为星号(*),或者说是被和谐掉了。请注意命案词只有小写字母,文本中如果有大写字母,一样要被和谐。如果敏感词只是单词的一部分,当前单词也要被和谐。
例如:敏感词是 eat
Your English is Great将被和谐为 Your English is *****.
本题敏感词 greatwall,each,we
实现:
Cross the greatwall,We can reach every corner of the world.
被和谐为:
Cross the ********* ,** can ***** every corner of the world.
就本题而言,不考虑其他情况,单纯实现将题目所给字符串按要求和谐,关键思路如下:
1、依次将敏感词与原字符串进行比对
2、如出现敏感词,将整个单词替换
3、替换后的星星数量为原单词的长度
完整代码如下:
var str = 'Cross the greatwall,We can reach every corner of the world.'
//Cross the ********,** can ***** every corner of the world.
var mgArr = ["we", "greatwall", "each"]; //敏感词数组
for (var i = 0; i < mgArr.length; i++) {
var reg = new RegExp('\\w*'+mgArr[i]+'\\w*', "ig"); //构造一个正则对象,敏感词前后有无字母或有多少字母(即只要出现敏感词整个单词匹配),全局忽略大小写
var star='';
for(var j=0;j<str.match(reg)[0].length;j++){ //str.match(reg)[0]为正则匹配的原字符串中的单词
star +='*'; // *数量为单词长度
}
str = str.replace(reg, star);
}
console.log(str);
欢迎讨论指正!
题目如下:
题目描述:
大部分论坛、网站等,为了方便管理,都进行了关于敏感词的设定。在多数网站,敏感词一般是指带有敏感政治倾向、暴力倾向、不健康色彩的词或不文明语,也有一些网站根据自身实际情况,设定一些只适用于本网站的特殊敏感词。比如,当你发帖的时候带有某些事先设定的词时,这个贴是不能发出的。或者这个词被自动替换为星号(*),或者说是被和谐掉了。请注意命案词只有小写字母,文本中如果有大写字母,一样要被和谐。如果敏感词只是单词的一部分,当前单词也要被和谐。
例如:敏感词是 eat
Your English is Great将被和谐为 Your English is *****.
本题敏感词 greatwall,each,we
实现:
Cross the greatwall,We can reach every corner of the world.
被和谐为:
Cross the ********* ,** can ***** every corner of the world.
就本题而言,不考虑其他情况,单纯实现将题目所给字符串按要求和谐,关键思路如下:
1、依次将敏感词与原字符串进行比对
2、如出现敏感词,将整个单词替换
3、替换后的星星数量为原单词的长度
完整代码如下:
var str = 'Cross the greatwall,We can reach every corner of the world.'
//Cross the ********,** can ***** every corner of the world.
var mgArr = ["we", "greatwall", "each"]; //敏感词数组
for (var i = 0; i < mgArr.length; i++) {
var reg = new RegExp('\\w*'+mgArr[i]+'\\w*', "ig"); //构造一个正则对象,敏感词前后有无字母或有多少字母(即只要出现敏感词整个单词匹配),全局忽略大小写
var star='';
for(var j=0;j<str.match(reg)[0].length;j++){ //str.match(reg)[0]为正则匹配的原字符串中的单词
star +='*'; // *数量为单词长度
}
str = str.replace(reg, star);
}
console.log(str);
欢迎讨论指正!
相关文章推荐
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Question 10: Given the following program snippet, what can we conclude about the use of dynamic_cast in C++?
- Culture of the Great Wall
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- The maximum number of processes for the user account running is currently , which can cause performance issues. We recommend increasing this to at least 4096.
- Home Training Clients Blog Tech Corner Contact Exploring the world of Android :: Part 2
- How can we double the data rate of 802.11a?
- What can we do in the CacheMetaData Method of Activity
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- We're on the cusp of deep learning for the masses. You can thank Google later
- Nothing in the world can take the place of Persistence
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- Can we call the Method of a controller from another controller in asp.net MVC?
- To select the file to upload we can use the standard HTML input control of type
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- How can we identify the weights of different models for ensemble?
- Study on the Reconstruction of Ancient Great Wall of Shanhaiguan Area
- Can you explain the result of the following example? Oh, my!
- [导入]JavaScript:The World's Most Misunderstood Programming Language