AS3 - Cómo escapar correctamente de expresiones regulares
Frecuentes
Visto 792 veces
2
I've got a couple of regex's in AS3 that are defined with the inline AS3 regex syntax like:
/\/\*[\s\S]*?\*\//gm
I want to convert that to the AS3 RegEx class constructor like:
new RegExp('/\/\*[\s\S]*?\*\//', "gm")
I'm not sure if I have to escape the backslashes or not. Do you know how to convert it?
/** ------------------------- Regular Expressions ---------------------------- */
public static var multiLineCComments:RegExp = /\/\*[\s\S]*?\*\//gm;
public static var singleLineCComments:RegExp = /\/\/.*$/gm;
public static var singleLinePerlComments:RegExp = /#.*$/gm;
public static var doubleQuotedString:RegExp = /"([^\\"\n]|\\.)*"/g;
public static var singleQuotedString:RegExp = /'([^\\'\n]|\\.)*'/g;
public static var multiLineDoubleQuotedString:XRegExp = new XRegExp('"([^\\\\"]|\\\\.)*"', 'gs');
public static var multiLineSingleQuotedString:XRegExp = new XRegExp("'([^\\\\']|\\\\.)*'", 'gs');
public static var xmlComments:RegExp = /(<|<)!--[\s\S]*?--(>|>)/gm;
public static var url:RegExp = /\w+:\/\/[\w-.\/?%&=:@;]*/g;
2 Respuestas
1
I understood how to do it.
Delete the first and last
/
which is the part of the AS3 regex syntax.Place the ending options such as
g
orgm
in the 2nd argument.And lastly escape every backslash (
\
) by replacing it with 2 backslashes
Entonces en resumen:
- Inline syntax :
/\/\*[\s\S]*?\*\//gm
- RegExp syntax :
new RegExp ("\\/\\*[\\s\\S]*?\\*\\/", 'gm')
Respondido 28 ago 12, 13:08
0
You could do this another way:
private static const pattern:RegExp = /\/\*[\s\S]*?\*\//;
public static var regex:XRegExp = new XRegExp(pattern.source, "gm");
Respondido 28 ago 12, 13:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas regex actionscript-3 syntax or haz tu propia pregunta.