简体   繁体   English

正则表达式 C# 与 javascript 不一致

[英]Regex C# not working consistent with javascript

I have an issue about regex This is my string str = 'tât" and I'm using regex for我有一个关于正则表达式的问题这是我的string str = 'tât"我正在使用正则表达式

  • javascript: javascript:

    str = str.replace(/[^\w\\-]+/g, ''); => result: tt

  • c#: C#:

    str = (new Regex(@"[^\w\\-]+")).Replace(str, ""); => result: tât

I want to make result of C# like javascript, Please help me.我想像javascript一样制作C#的结果,请帮助我。

Thanks so much非常感谢

The default.Net implementation of Regex is slightly different from the Javascript implementation. Regex 的默认 .Net 实现与 Javascript 实现略有不同。

Differences are described on on the Microsoft website . Microsoft 网站上描述了不同之处。

To use Javascript/ECMAscript rules in.Net:在 .Net 中使用 Javascript/ECMAscript 规则:

   str = Regex.Replace(str, @"[^\w\\-]+", "", RegexOptions.ECMAScript);

You could try using a Alphabetic range like that:您可以尝试使用这样的字母范围:

str= (new Regex(@"[^A-Za-z0-9]+")).Replace(str, ""); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM