I am trying to retrieve a regex expression from database and pass it on to client (Browser). But the regex expression is getting slightly modified in between due to intermidiate parsing and regex expression processing in C#. Can anyone please tell me what I need to do inorder to pass the regex correctly to client. I have the following piece of code
private static readonly Regex defaultObjectPattern = new Regex(@"([^\w\.$\]])(\['?[$\w \s]*'?\][^\.\[])", RegexOptions.Compiled);
var parsedFormula = new StringBuilder(defaultObjectPattern.Replace(rule.Rule, "$1item$2"));
where rule.Rule
is
"Error(\"![Item].[XYZ ID].match(/^20[^\s]{4,}$|^$/)\", \"Invaid XYZ ID\",\"XYZ ID entered is Invalid. Please obtain a valid XYZ ID from your Supervisor or delete this entry.\", \"XYZ ID\",\"All\")"
By the time above instruction is complete the regex expression in the match method is getting modified as /^20\['^\s']{4,}$|^$/
. Single quotes are getting added within the Square brackets.
The exact string I am storing in the database is
'Error("![Item].[XYZ ID].match(/^20[^\s]{4,}$|^$/)", "Invaid XYZ ID","XYZ ID entered is Invalid. Please obtain a valid XYZ ID from your Supervisor or delete this entry.", "XYZ ID","All")'
I cannot change the defaultObjectPattern
as it is used for lot of other things. But I need to get the regex expression in match method without getting modified (without single quotes getting added).
Thanks in advance for your help.