PDA

View Full Version : RegExp (again)


xwielder
11-06-2007, 03:04 PM
var pattern1:RegExp = /"/gi; // " double quote
var pattern2:RegExp = /'/gi; // ' single quote
var pattern3:RegExp = /</gi; // < less than
var pattern4:RegExp = />/gi; // > greater than
var pattern5:RegExp = /(\/)/gi; // / forward slash
var pattern6:RegExp = /(\\)/gi; // \ back slash

questionSstring = questionTextField.text;
questionSstring = questionSstring.replace(pattern1, "&quot;"); // " double quote
questionSstring = questionSstring.replace(pattern2, "&apos;"); // ' single quote
questionSstring = questionSstring.replace(pattern3, "&lt;"); // < less than
questionSstring = questionSstring.replace(pattern4, "&gt;"); // > greater than
questionSstring = questionSstring.replace(pattern5, "&#.47;"); // / forward slash
questionSstring = questionSstring.replace(pattern6, "&#.92;"); // \ back slash


Surely there's a way to make 1 pattern instead of 6 paterns. As you can though, for now, I'm running a string thru 6 seperate times. Do I have to filter 6 times or can I filter only once? What would be the RegExp code to consolidate?

xwielder
11-07-2007, 11:15 AM
Anyone?

matbury
11-07-2007, 11:34 AM
It looks like you're trying to format your String for server side script. It's worth having a look to see if there's a standard Regular Expression ofr it already. Sometimes you can just refer to a preset RegExp with a method name, but essentially it'll just run through what you're writing here anyway.

Yeah, it would be nice to fit it into one line though. I'm not very good with RegExps!

xwielder
11-08-2007, 11:47 AM
What I'm actually doing is prepping the user-input text to go into an XML file. So, to keep those special characters from messing up the XML, I have to convert the user-input " ' < > / \ into html characters.

Here's my function. (Not that this will help, but it's fun to look at.)


function saveAs (event:Event):void
{
var pattern1:RegExp = /"/gi; // " double quote
var pattern2:RegExp = /'/gi; // ' single quote
var pattern3:RegExp = /</gi; // < less than
var pattern4:RegExp = />/gi; // > greater than
var pattern5:RegExp = /(\/)/gi; // / forward slash
var pattern6:RegExp = /(\\)/gi; // \ back slash

var stringArray:Array = new Array();
var catArray:Array = new Array();

for (var zz:Number = 56; zz > -1; zz--)
{
var questionTextField:TextField = root["question_" + zz + "_txt"];
var answerTextField:TextField = root["answer_" + zz + "_txt"];
var catTextField:TextField = root["topic_" + zz + "_txt"];

var questionSstring:String = "question_" + zz;
var answerSstring:String = "answer_" + zz;
var catSstring:String = "topic_" + zz;

if (questionTextField != null)
{
questionSstring = questionTextField.text;
questionSstring = questionSstring.replace(pattern1, "&quot;"); // " double quote
questionSstring = questionSstring.replace(pattern2, "&apos;"); // ' single quote
questionSstring = questionSstring.replace(pattern3, "&lt;"); // < less than
questionSstring = questionSstring.replace(pattern4, "&gt;"); // > greater than
questionSstring = questionSstring.replace(pattern5, "/"); // / forward slash
questionSstring = questionSstring.replace(pattern6, "\"); // \ back slash

answerSstring = answerTextField.text;
answerSstring = answerSstring.replace(pattern1, "&quot;");
answerSstring = answerSstring.replace(pattern2, "&apos;");
answerSstring = answerSstring.replace(pattern3, "&lt;");
answerSstring = answerSstring.replace(pattern4, "&gt;");
answerSstring = answerSstring.replace(pattern5, "/");
answerSstring = answerSstring.replace(pattern6, "\");

stringArray[zz] = ("<row question=\"" + questionSstring + "\" answer=\"" + answerSstring + "\"" + " val=\"200\"" + " dd=\"" + redGreen[zz] + "\"/>\r\n");
}
if ((zz > 0) && (zz <= 5) && (catTextField != null))
{
catSstring = catTextField.text;
catSstring = catSstring.replace(pattern1, "&quot;");
catSstring = catSstring.replace(pattern2, "&apos;");
catSstring = catSstring.replace(pattern3, "&lt;");
catSstring = catSstring.replace(pattern4, "&gt;");
catSstring = catSstring.replace(pattern5, "/");
catSstring = catSstring.replace(pattern6, "\");

catArray[zz] = ("<cat name=\"" + catSstring + "\"/>\r\n");
}
}

var line1:String = ("<?xml version =\"1.0\" encoding=\"utf-8\" ?>\r\n<game name=\"Default Game\">\r\n");
var line2:String = ("<categories>\r\n<cat/>\r\n");
var line8:String = ("</categories>\r\n");
var line9:String = ("<column1>\r\n<row/>\r\n");
var line10:String = ("</column1>\r\n");
var line11:String = ("<column2>\r\n<row/>\r\n");
var line12:String = ("</column2>\r\n");
var line13:String = ("<column3>\r\n<row/>\r\n");
var line14:String = ("</column3>\r\n");
var line15:String = ("<column4>\r\n<row/>\r\n");
var line16:String = ("</column4>\r\n");
var line17:String = ("<column5>\r\n<row/>\r\n");
var line18:String = ("</column5>\r\n");
var line19:String = ("</game>\r\n");
var line20:String = ("\r\n<!-- This VA Jeopardy file was created by VA-Jeopardy.air -->");

var theWholeDamString:String = (line1 + line2 + catArray[1] + catArray[2] + catArray[3] + catArray[4] + catArray[5] + line8 + line9 + stringArray[11] +
stringArray[12] + stringArray[13] + stringArray[14] + stringArray[15] + line10 + line11 +
stringArray[21] + stringArray[22] + stringArray[23] + stringArray[24] + stringArray[25] +
line12 + line13 + stringArray[31] + stringArray[32] + stringArray[33] + stringArray[34] +
stringArray[35] + line14 + line15 + stringArray[41] + stringArray[42] + stringArray[43] +
stringArray[44] + stringArray[45] + line16 + line17 + stringArray[51] + stringArray[52] +
stringArray[53] + stringArray[54] + stringArray[55] + line18 + line19 + line20);

// Write the file
dskTopFileStream.writeUTFBytes (theWholeDamString);

// Close the file
dskTopFileStream.close ();
}

xfreece
11-08-2007, 12:15 PM
In AS3.0 you have great support for writing and reading xml already built in (like RegExp).. You don't need all that regexp stuff.

xwielder
11-08-2007, 12:25 PM
In AS3.0 you have great support for writing and reading xml already built in (like RegExp).. You don't need all that regexp stuff.

Thanks for that great insight. Thanks also for the provided example. I'd be totally lost if it weren't for your detailed and helpful post.

xfreece
11-09-2007, 05:07 AM
Ok. Sorry then for the short response (Thougth own research was something everyone did). However I'll just skip that description and ask another question, why doesn't you use CDATA tag?

<![CDATA[You can write whatever you want here without messing up the XML for the XMLParser < <dsa Das da !''""# #*!> ....]]>

(http://www.w3schools.com/xml/xml_cdata.asp)

matbury
11-09-2007, 12:42 PM
Great link xfreece. Bookmarked it.

xwielder, I think I know what you're going through. It's quite a change from AS 2.0, especially with XML. It used to be so difficult and complicated but they've made things really easy now. I few good tutorials on AS 3.0 should sort things out. There's a nice one here:

http://www.adobe.com/devnet/flash/articles/filtering_data_e4x.html

Not necessarily on topic in your case, but it gives you a feel for the new way it works. Also, check out the AS 3.0 Language reference - I don't think it's very well organised and the examples aren't as comprehensive as they should be, but it's what we've got until something better comes along.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/

Good luck!