Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between C# and JavaScript Regular Expressions? [closed]

Are C# and JavaScript Regular Expressions different?

Is there a list of these differences?

like image 314
Michael Stum Avatar asked Oct 20 '10 21:10

Michael Stum


People also ask

What is diff between C and C++?

Conclusion. In a nutshell, the main difference between C and C++ is that C is a procedural with no support for objects and classes whereas C++ is a combination of procedural and object-oriented programming languages.

Is C or C++ better?

Compared to C++, C is the simpler and ultimately faster programming language. C is procedural and does not support classes and objects, meaning it has less functionality than C++. This allows you to spend more time focusing on what you can do with C's libraries, especially at the OS level.

What are the different versions of C?

Let's continue with a discussion of all the five different standards of C — K&R C, ANSI C, C99, C11 and Embedded C. For the purposes of our discussion, the compiler used is the gcc C compiler from the GNU Compiler Collection (GCC).

What is the main difference between C and C language?

There is a major difference between C and C++. The C language is a procedural one that provides no support for objects and classes. On the other hand, the C++ language is a combination of object-oriented and procedural programming languages.


2 Answers

Here's a difference we bumped into that I haven't seen documented anywhere, so I'll publish it, and the solution, here in the hope that it will help someone.

We were testing "some but not all" character classes, such as "A to Z but not Q, V or X", using "[A-Z-[QVX]]" syntax. Don't know where we found it, don't know if it's documented, but it works in .Net.

For example, in Powershell, using the .Net regex class,

[regex]::ismatch("K", "^[A-Z-[QVX]]$") 

returns true. Test the same input and pattern in JavaScript and it returns false, but test "K" against "^[A-Z]$" in JavaScript and it returns true.

You can use the more orthodox approach of negative lookahead to express "A to Z but not Q, V or X", eg "^(?![QVX])[A-Z]$", which will work in both Powershell and (modern) JavaScript.

Given Ben Atkin's point above about IE6 and IE7 not supporting lookahead, it may be that the only way to do this in a fool-proof (or IE7-proof) way is to expand the expression out, eg "[A-Z-[QVX]" -> "ABCDEFGHIJKLMNOPRSTUWYZ". Ouch.

like image 77
Francis Norton Avatar answered Sep 26 '22 15:09

Francis Norton


First, some resources:

  • Mozilla Development Center JavaScript Guide: Regular Expressions
  • .NET Framework Regular Expressions - see the links at the bottom of the page

Here are a few differences:

  • Lookahead is not supported in IE6 and IE7. (Search for x(?=y) in the MDC guide for for examples.)
  • JavaScript doesn't support named capture groups. Example: (?<foo>)
  • The list of metacharacters supported by JavaScript is much shorter.
like image 21
Benjamin Atkin Avatar answered Sep 27 '22 15:09

Benjamin Atkin