Here is the one the ClientAPI uses. (ClientAPI.GetSafeJSString)
''' -----------------------------------------------------------------------------
''' <summary>
''' Escapes string to be safely used in client side javascript.
''' </summary>
''' <param name="strString">String to escape</param>
''' <returns>Escaped string</returns>
''' <remarks>
''' Currently this only escapes out quotes and apostrophes
''' </remarks>
''' <history>
''' [Jon Henning] 2/17/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Function GetSafeJSString(ByVal strString As String) As String
If Len(strString) > 0 Then
Return System.Text.RegularExpressions.Regex.Replace(strString, "(['""\\])", "\$1")
Else
Return strString
End If
End Function
However, as you just pointed out to me, I do not cover linefeeds appropriately. I am not sure that you need to cover tabs. Also, I am no regular expression expert, so if someone wants to provide the right expression to cover the cases that SuperSKa notes above please post them here.