Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top