4

My question is as in the title. If two syntaxes are different then how to convert an regex in asp.net to javascript.

Really appreciated for anyone's answer.

Edit: I have the Regex to test email address like this:

"((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\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)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))"

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

new RegeExp(regex).test(email);

It throws an syntax error:

SyntaxError: Invalid regular expression

So how can I use that terible regex in javascript?

1
  • If you use a string to construct a regex in JavaScript, you need to double the backslashes, for instance. There are also some other syntactical changes. See my edited answer for a converted regex. Commented Sep 20, 2012 at 12:08

4 Answers 4

2

They are different. To test how it would run in JavaScript you can do something like:

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

in .NET that will make it pretty much equivalent. If you can, run some code through it. That will help you to determine if they are working the same.

For writing and testing in JavaScript's RegEx implementation I highly recommend http://regexpal.com/ It runs 100% in browser and even highlights in real time for you.

Sign up to request clarification or add additional context in comments.

Comments

1

Apart from syntax differences, there are a lot of things that .NET has and JavaScript doesn't:

  • lookbehind assertions
  • support for Unicode properties (which also affects shorthand character classes like \w, word boundaries like \b etc.)
  • recursive regexes
  • forward references
  • atomic grouping
  • conditionals
  • POSIX character classes
  • named capturing groups
  • a DOTALL option
  • multiline regexes
  • many other small details as seen here

So if your .NET regex uses any of those features, they need to be reworked substantially or (in the case of lookbehind and recursion) cannot be translated to JavaScript.

Edit:

In the case of your regex, it seems that mainly syntax changes are necessary (a task that I use RegexBuddy for). The resulting regex is this:

var myregexp = /((([a-z]|\d|[!#$%&'*+\-\/=?\^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\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)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))/;

I have no idea what this monster is supposed to be doing, so you need to test if it matches everything the way you expect it.

1 Comment

Really thankful for your discover:). It works as I expect. The regex looks very verbose, but at the moment I need to use it. And now I have known a strong tool to work with regex. Thanks again.
1

Javascript uses XRegExp and .NET uses Microsofts. They are different.

XRegExp seems to have fewer features.

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

1 Comment

No. XRegExp is a third-party extension by Steve Levithan to JavaScript's native regex engine. It's not part of JavaScript.
0

Regular expressions are almost the same, but the syntax for usage can differ in different programming languages.

RegExBuddy is a good tool to write RegEx and get the usage in most of the popular programming languages.

2 Comments

I would argue that JavaScript's native regexes are vastly inferior to .NET's regexes. They are not in the same ballpark, well, not even in the same league.
I dont want to argue buddy. I meant "almost same"

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.