【问题标题】:Regex Replace Special Characters gives different results in JS and Python正则表达式替换特殊字符在 JS 和 Python 中给出不同的结果
【发布时间】:2019-10-11 07:58:51
【问题描述】:

我正在尝试使用正则表达式替换所有特殊字符并比较 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,我该怎么办?

【问题讨论】:

  • The particularly important problem is due to the letter 'c' in the word crypto。那是因为它不是拉丁文 c 而是西里尔文 с
  • с 是一个西里尔字母。默认情况下,Python 3 中的 \W 支持 Unicode。
  • 仅供参考,我粘贴了 here 这个词来查看字符名称(我没有网站从属关系)。

标签: javascript python regex unicode special-characters


【解决方案1】:

с 是一个西里尔字母。 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 文档:

使\w\W\b\B\d\D\s\S 只执行 ASCII 匹配,而不是完整的 Unicode 匹配。这仅对 Unicode 模式有意义,对于字节模式被忽略。对应内联标志(?a)

【讨论】:

  • 谢谢!如果您有任何想法,如何使角色不会在 JS 中被丢弃,如果您在 JS 中使用标志 u 它仍然会丢弃字母 c
  • 如果你使用这个字符串 s = 'hernández' 并在 js 和 python 中运行相同的正则表达式来替换 \W,python 版本由于 unicode 支持不会下降,但 JS 版本会
  • @PirateApp 你的意思是让\W 在JS RegExp 中识别Unicode?在 Chrome 中,replace(/[^\d\p{L}]/gu, ' ') 可以使用,但在 FF 和 IE 中不起作用。对于较旧的浏览器/JS 环境,请使用Javascript + Unicode regexes 中列出的方法
猜你喜欢
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多