[英]javascript regular expression match exclude space and special charcters
Am having set of restricted keywords 正在设置一组受限关键字
in my comment / message posting block should not allow the restricted words which i defined. 在我的评论/消息发布块中,不应允许使用我定义的受限词。
Eg: keyword is "facebook". 例如:关键字是“ facebook”。
facebook, FaceBook, facebook, f*a*c*e*b*o*o*k, facebook, (face book),'facebook' these words should not allow to post. facebook,FaceBook,facebook,f * a * c * e * b * o * o * k,facebook,(脸书),“ facebook”这些词不应发布。
Any ideas using regular expression in java script would be appreciated. 在Java脚本中使用正则表达式的任何想法将不胜感激。
I'm not sure this is a good idea but all the words you show could be detected using 我不确定这是个好主意,但可以使用
var isFacebook = /f\W*a\W*c\W*e\W*b\W*o\W*o\W*k/i.test(str);
Note that you can easily generate such a pattern from a word, which makes it easy to extend with a dictionary : 请注意,您可以轻松地从单词中生成这样的模式,从而可以轻松地通过字典进行扩展:
var r = new RegExp("facebook".split('').join('\\W*'), 'i');
Try this regex: 试试这个正则表达式:
f[^a-zA-Z0-9]?a[^a-zA-Z0-9]?c[^a-zA-Z0-9]?e[^a-zA-Z0-9]?b[^a-zA-Z0-9]?o[^a-zA-Z0-9]?o[^a-zA-Z0-9]?k
It will match the following: 它将符合以下条件:
facebook
f a c e b o o k
f-a-c-e-b-o-o-k
f*a*c*e*b*o*o*k
But will not match the following: 但将不符合以下条件:
facesbooks
ffaceebbookss
You can use a regex similar to the one provided to detect such words. 您可以使用类似于提供的正则表达式来检测此类单词。
If you mean that you want to filter a given word, surrounded by word-boundary and with possible special characters separating the word's letters: 如果您要过滤给定的单词,请用单词边界将其包围,并用可能的特殊字符分隔该单词的字母:
var keyword="facebook",
specialCharClass="[*-]",
regex;
regex= new RegExp("\\b" + keyword.replace(/(?:)/g,specialCharClass+'?') + "\\b",'g');
"hi(facebo-ok)pie".replace(regex,"__________"); //returns "hi(__________)pie"
However, there are always ways around word filters (faceb00k for example). 但是,总有绕过字过滤器的方法(例如,faceb00k)。
If your goal is to block, not to strip, I would implement this as a two step process. 如果您的目标是阻止而不是剥离,那么我将其实现为两步过程。
That way you seperated your concerns of blocking a certain list of words, and making sure that you're checking the actual text. 这样,您就不必担心阻止某些单词列表,并确保您正在检查实际文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.