Convert JavaScript Regex to C#

Convert JavaScript Regex to C#

Remove / at the start and the end of the string.

How to convert JavaScript regex to c# regex

Regex itself is an standard. So this regex should work equally on C# too. But what you have to do is escaping this expression. Do it by hand or let it do by websites like https://www.freeformatter.com/java-dotnet-escape.html

How to convert JavaScript regex to c# regex?

In pattern3, you have regex \&\&| which matches basically everything.
Just remove the last pipe like this and get what you want:

String pattern3 = @"\&\&";

Converting Javascript RegEx to C# Regex

Note that RegexOptions.ECMAScript just makes sure shorthand character classes (here, \w and \s) only match ASCII letters, digits and whitespace. You can't expect this option to "convert" the whole pattern for use in .NET regex library.

Here, [^] construct was used in JS regex to match any char. You may use . with a RegexOptions.Singleline option (and then you will have to remove the RegexOptions.ECMAScript option) instead of [^], or just use [\s\S] to match any char:

public static void Main()
{
string pattern = @"\\.|\.+|\w+|[^\w\s]";
string input = @"hello world.";

foreach (Match m in Regex.Matches(input, pattern, RegexOptions.Singleline))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}

See the C# demo, its output:

'hello' found at index 0.
'world' found at index 6.
'.' found at index 11.

NOTE: \w and \s are Unicode aware in .NET regex, the match all Unicode letters with some diacritics, too. If you only want to handle ASCII, use

string pattern = @"\\.|\.+|[A-Za-z0-9_]+|[^A-Za-z0-9_\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]";

More details

  • Word Character: \w in .NET regex
  • White-Space Character: \s in .NET regex

Convert po box javascript regex to c# regex

Since there is an /i modifier, you need to use RegexOptions.IgnoreCase. All \/ should be written as / as the forward slash is not a special regex character and not a regex delimiter (these do not exist in C# regex). Youl also need to use a verbatim string literal (i.e. @"pattern") form when converting a JS regex literal to C# pattern.

So, use

string PoBoxRegex = @"(?i)^ *((#\d+)|((box|bin)[-. /\\]?\d+)|(.*p[ .]? ?(o|0)[-. /\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)";

The (?i) is an inline case insensitive flag.

Convert JavaScript RegEx to C# RegEx for email validation

As you are working with C#, you should be able to validate email addresses using the System.Net.Mail.MailAddress class. You can use it like this:

public bool IsValidEmailAddress(string emailAddress)
{
try
{
MailAddress m = new MailAddress(emailAddress);
return true;
}
catch (FormatException)
{
return false;
}
}

Sometimes you can go without regexes, especially if you're not an expert.

Regex gives different result in javascript and .NET

Problem with the example that you've given, you ommited space from list of allowed characters. For your example this pattern works:

var pattern = @"^[^&<>;]+\\.";

How to convert C# regex to typescript regex?

The C# code you've provided uses C#'s Verbatim string syntax, which doesn't require escaping of backslashes. To convert it to a normal string literal, which would be the same in C# and JavaScript, you can remove the @ symbol at the front and then escape backslashes by adding another backslash before them:

 "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z-])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$"

To use this as a JavaScript Regex, you pass it to the RegExp constructor:

let emailRegularExpression = new RegExp("^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z-])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$");

Or, even better, you can just use JavaScript's literal regex syntax (without escaping backslashes:

let emailRegularExpression = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z-])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;

This works as specified for the cases you've specified:

  • emailRegularExpression.test("adasd457@sdjf45.idie"): true
  • emailRegularExpression.test("ex-sdf@sadf.co333"): false
  • emailRegularExpression.test("j²coolio@sadf.co333"): false (special character)


Related Topics



Leave a reply



Submit