我正在尝试使用 Regex 替换所有特殊字符并在 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)
这给了我以下输出
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 中没有被移除 谁能告诉我为什么,我也不希望 JS 删除字母 c,我该怎么办?
最佳答案
с
是西里尔字母。 Python 3 中的 \W
默认支持 Unicode,但在 JavaScript 中不支持。
要在 Python 中也删除它,传递 re.ASCII
as the flag :
import re
regex_special_characters = re.compile(r'\W', re.ASCII)
regex_special_characters.sub(' ', text)
re.ASCII
文档中的更多详细信息:
Make
\w
,\W
,\b
,\B
,\d
,\D
,\s
and\S
perform ASCII-only matching instead of full Unicode matching. This is only meaningful for Unicode patterns, and is ignored for byte patterns. Corresponds to the inline flag(?a)
.
关于javascript - 正则表达式替换特殊字符在 JS 和 Python 中给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58336755/