Is the regex syntax in asp.net and javascript the same?

My question is the same as in the title. If the two syntaxes are different, then how to convert the regular expression in asp.net to javascript.

Really appreciated for any answer.

Change I have a regex to validate an email address as follows:

"((([az]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([az]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))" 

It works fine in .net. But when I emulate a regex and use in javascript:

 new RegeExp(regex).test(email); 

It throws a syntax error:

 SyntaxError: Invalid regular expression 

So how can I use this regex in javascript?

+4
source share
4 answers

Besides the syntax differences, there are many things that .NET and JavaScript have:

  • lookbehind statements
  • support for Unicode properties (which also affects abbreviated character classes such as \w , word boundaries such as \b , etc.)
  • recursive regular expressions
  • direct links
  • atomic grouping
  • conditional
  • POSIX Character Classes
  • named capture groups
  • DOTALL option
  • multiline regular expressions
  • many other small details as shown here

So, if your .NET regular expression uses any of these functions, they must be essentially reworked or (in the case of lookbehind and recursion) cannot be translated into JavaScript.

Edit:

In the case of your regex, it seems like syntax changes are needed (the task I'm using RegexBuddy for). The resulting regular expression is as follows:

 var myregexp = /((([az]|\d|[!#$%&'*+\-\/=?\^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([az]|\d|[!#$%&'*+\-\/=?\^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))/; 

I don’t know what this monster should do, so you need to check if it matches all of what you expect.

+1
source

They are different. To test how it will work in JavaScript, you can do something like:

 Regex.IsMatch("input", "regex", RegexOptions.ECMAScript) 

in .NET, which will make it pretty much equivalent. If you can, run some code with it. This will help you determine if they work the same.

For writing and testing in the implementation of JavaScript RegEx, I highly recommend http://regexpal.com/ It works 100% in the browser and even provides real-time highlighting for you.

+2
source

Javascript uses XRegExp, and .NET uses Microsoft. They are different.

XRegExp seems to have fewer features.

Wikipedia has a comparison table http://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines

+1
source

Regular expressions are almost the same, but the usage syntax may vary in different programming languages.

RegExBuddy is a good tool for writing RegEx and getting use in most popular programming languages.

0
source

Source: https://habr.com/ru/post/1435185/


All Articles