Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert c# regex to javascript regex

The following regular expression is workable using C# reg ex:

^(?<survey>#_surveyForm.+)|#(?:(?<url>http.+\.\w{3,4}).+_surveyForm=\w+)$

It matches string such as:

#http://localhost/tableaux-p145717.htm=&_surveyForm=second

or

#_surveyForm=second

I used named capturing groups.

I know Javascript doesn't take advantage of named capturing groups (instead it uses \1, \2, etc.). Besides the syntax is slightly different from the one above.

How can I make that reg ex javascript-compliant?

Thanks in advance,

R.

like image 540
roland Avatar asked Mar 07 '26 14:03

roland


1 Answers

As you said, JavaScript doesn't support named captures. You have to change those into "normal" capturing groups and refer to them by number instead of by name:

/^(#_surveyForm.+)|#(?:(http.+\.\w{3,4}).+_surveyForm=\w+)$/

You should also be aware that \w only matches ASCII alphanumerics in JavaScript whereas it matches Unicode alnums in .NET.

like image 194
Tim Pietzcker Avatar answered Mar 09 '26 03:03

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!