views:

254

answers:

2

I have a user supplied text and I need to prepend all backslashes and single quotes with a backslash. How to do that?

+4  A: 
var string:String = "something 'is' \\fishy here";
trace(string);//something 'is' \fishy here
string = string.replace(/(['\\])/g, "\\$1");
trace(string);//something \'is\' \\fishy here
Amarghosh
+2  A: 

Amarghosh's answer is spot on. If you want an easy way to test out AS3 regex, Grant Skinner's Regexr is awesome.

Joel Hooks