Q:

How to insert a regular expression pattern from resources into a string.Format?

When generating the JavaScript code that uses a regular expression, I observe problems with inserting the regular expression pattern from a string variable. The content is read from a resource file.
It seems to me that the problem is with backslash interpretation when inserted into the template string using string.Format. The JavaScript seems to be fine; however, the regular expression does not match the correct text.
The regular expression pattern defines how a company email should look like, and it is defined in the resource file (that is read into a string variable first). It looks like this: "\\w+\\.\\w+@company.com". It should match users with e-mail like john.doe@company.com. If it does not match, the JavaScript code asks for further confirmation.
The same JavaScript (as shown below) worked fine when it was hardwired. The problem is related to the dynamic definition of the JavaScript code.
Context: Having the even handler for an asp.net button, I want to add the client side code. The pattern is in the resources because the code should be reused for more companies. The confirmation text is in the resources because of internationalization.
    protected void btnSave_Init(object sender, EventArgs e)
    {
        ASPxButton btn = sender as ASPxButton;
        string regex = GetLocalResourceObject("btnSave.RegEx").ToString();
        string txt = GetLocalResourceObject("btnSave.ConfirmText").ToString();
        txt = txt.Replace("\r\n", "\\n");   // must be a single line for JavaScript
        string fo = @"
function(s, e) {{
    var rex = new RegExp('{0}', 'i');
    if (!rex.test(email.GetText()))
        e.processOnServer = confirm('{1}');
}}";
        btn.ClientSideEvents.Click = String.Format(fo, regex, txt);
    }

Update: The string written into the resource file looks like \w+\.\w+@company\.com (single backslashes, the escape of the last dot fixed). When read to a string variable, the backslashes are inside as normal characters. The text retreived from the form is like jiri.novak@company.com. The line that creates the regular expression object was changed to var rex = /^(?:{0})$/i; as suggested. There was another minor bug that was not shown.

A:

The point is that you need to pass a literal \w+\.\w+@company\.com string. It means that the backslashes must be literal backslash symbols, not those backslashes that form escape sequences (like \n for a newline, \r for a carriage return, etc).
So, there are 2 ways to create a regex in JS: a constructor notation (new RegExp("\\w+\\.\\w+@company\\.com")) or a regex literal notation (e.g. /\w+\.\w+@company\.com/, no additional backslash escaping necessary as the \ is treated as a literal backslash in the regex literals). Besides, in regex literals, you must escape / symbol.
Note you need to match the whole string against the regex, so you need ^ and $ anchors, too. 
Thus, you need
var rex = /^(?:{0})$/i;

and when passing a regex, replace all unescaped / with \/.

