简体   繁体   中英

How to convert JavaScript regex to c# regex

How to convert the below Java Script to C#.

function test() {

var patKeyValue = /"(.*)"\s*=\s*"(.*)"/; // "key" = "value";            
var patCommentSingle = /\/\/(.*)/; // single line comment
var patCommentBlock = /\/\*(.*)\*\//; /* block comment on one line */
var patCommentStart = /\/\*(.*)/; /* block comment start */
var patCommentEnd = /(.*)\*\//;  /* block comment end */

}

Replace the opening and closing / with ", and escape any enclosed " or \\ between the opening and closing /, using the back-slash \\ escape character. So the first example becomes:

var patKeyValue = "\"(.*)\"\\s*=\\s*\"(.*)\""; // "key" = "value";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM