4

I have an issue about regex This is my string str = 'tât" and I'm using regex for

  • javascript:

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

  • c#:

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

I want to make result of C# like javascript, Please help me.

Thanks so much

1

2 Answers 2

5

The default .Net implementation of Regex is slightly different from the Javascript implementation.

Differences are described on on the Microsoft website.

To use Javascript/ECMAscript rules in .Net:

   str = Regex.Replace(str, @"[^\w\\-]+", "", RegexOptions.ECMAScript);
Sign up to request clarification or add additional context in comments.

Comments

1

You could try using a Alphabetic range like that:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.