[英]Regex Replace Special Characters gives different results in JS and Python
我正在尝试使用正则表达式替换所有特殊字符并在 JavaScript (node.js v10.16.3)和 Python (3.7.x)之间进行比较
\t kickref, first really multi-level referral program on the сrypto market, has reached over 20 000 users just in 2 days after its start on september 28.
将句子拆分为字符只是为了查看 ASCII 代码给了我这个字符数组
'["\\t"," ","k","i","c","k","r","e","f",","," ","f","i","r","s","t"," ","r","e","a","l","l","y"," ","m","u","l","t","i","-","l","e","v","e","l"," ","r","e","f","e","r","r","a","l"," ","p","r","o","g","r","a","m"," ","o","n"," ","t","h","e"," ","с","r","y","p","t","o"," ","m","a","r","k","e","t",","," ","h","a","s"," ","r","e","a","c","h","e","d"," ","o","v","e","r"," ","2","0"," ","0","0","0"," ","u","s","e","r","s"," ","j","u","s","t"," ","i","n"," ","2"," ","d","a","y","s"," ","a","f","t","e","r"," ","i","t","s"," ","s","t","a","r","t"," ","o","n"," ","s","e","p","t","e","m","b","e","r"," ","2","8","."]'
这将是每个字母的 ASCII 码
'[9,32,107,105,99,107,114,101,102,44,32,102,105,114,115,116,32,114,101,97,108,108,121,32,109,117,108,116,105,45,108,101,118,101,108,32,114,101,102,101,114,114,97,108,32,112,114,111,103,114,97,109,32,111,110,32,116,104,101,32,1089,114,121,112,116,111,32,109,97,114,107,101,116,44,32,104,97,115,32,114,101,97,99,104,101,100,32,111,118,101,114,32,50,48,32,48,48,48,32,117,115,101,114,115,32,106,117,115,116,32,105,110,32,50,32,100,97,121,115,32,97,102,116,101,114,32,105,116,115,32,115,116,97,114,116,32,111,110,32,115,101,112,116,101,109,98,101,114,32,50,56,46]'
特别重要的问题是由于单词crypto中的字母“c”。 注意它的 ASCII 码在数组中是 1089
在 JS 中,我替换正则表达式的代码如下所示
const regexSpecialCharacters = new RegExp(/\W/, 'g');
text.replace(regexSpecialCharacters, ' ');
这产生以下句子
kickref first really multi level referral program on the rypto market has reached over 20 000 users just in 2 days after its start on september 28
字母 c 被删除在 Python 中,我的正则表达式做同样的事情看起来像这样
import re
regex_special_characters = re.compile(r'\W')
regex_special_characters.sub(' ', text)
这给了我以下 output
kickref first really multi level referral program on the сrypto market has reached over 20 000 users just in 2 days after its start on september 28
这里的字母 c 没有在 Python 中删除
с
是一个西里尔字母。 Python 3 中的\W
默认情况下可识别 Unicode,但在 JavaScript 中不存在。
要在 Python 中也将其删除,请将re.ASCII
作为标志传递:
import re
regex_special_characters = re.compile(r'\W', re.ASCII)
regex_special_characters.sub(' ', text)
re.ASCII
文档中的更多详细信息:
使
\w
、\W
、\b
、\B
、\d
、\D
、\s
和\S
执行仅 ASCII 匹配,而不是完整的 Unicode 匹配。 这仅对 Unicode 模式有意义,对于字节模式将被忽略。 对应于内联标志(?a)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.