as3 remove white space - actionscript-3

I am trying to remove / replace white space from a string in as3. The string comes from xml and than written into text field. to compare the strings I am trying to remove white spaces
var xmlSentence:String=myXML.SENTENCE[thisSentence];
var tfSentence=e.target.text;
var rex:RegExp = /\s+/;
trace(xmlSentence.replace(rex, "-"));
trace(tfSentence.replace(rex, "-"));
That code outputs like this:
She-has a dog
-She has a dog
I also tried different rex patterns. the problem is that though there are spaces in both string -which are same- it finds only one space but not the same one in both strings.
Could you help me to solve this problem
Thanks in advance

You need to use the g flag to indicate recursive changes
var rex:RegExp = /\s+/g ;
Within your Actionscript code, select the RegExp keyword, then goto the 'Help' menu and choose 'Flash Help' for more info on flags.

Related

Using Chrome.ahk library to fill out a field inside an iframe

Hello can anybody help me with trying to edit a field inside an ifram using CHROME.ahk
The following code works when I create an IE ojbect. This is the part I use to fill in the field.
workoder := test
frame := pwb.document.parentWindow.frames("uxtabiframe-1068-frame")
frame.document.GetElementsByName("ff_workordernum").item[0].Value := workorder
How do I do this using the chrome.ahk library.
and it doesn't work. I have tried several methods. Can anybody please help
I have trid this:
PageInstance.Evaluate("document.parentWindow.frames('uxtabiframe-1068-frame').document.GetElementsByName('ff_workordernum').item[0].Value = 'test' )
I get an error. I tried
iframeJs =
(
var iframe = document.getElementById('uxtabiframe-1068-frame');
var test2 = iframe.contentDocument..GetElementsByName('ff_workordernum').item[0].Value = 'test';
)
PageInstance.Evaluate(iframeJs)
I believe the problem with the first try you are trying to get the property "document" of the Iframe here:
frames('uxtabiframe-1068-frame').document
However Iframes have no such property, it's called "contentDocument"
I think this is silly and confusing that it is like this, just "document" makes more sense and should be the standard.
Also in the first try there is an opening double quote but there isn't a closing one.
In the second try there is a repeated "." here:
iframe.contentDocument..GetElementsByName
You only need one of them.
And also with the second try you've tried to assign a variable over multiple lines, everything must be on the same line and you can do that by using "`n" which is an escaped newline character.

Using JSON.stringify but SSJS variant in XPages

for an application I am building an administration panel where a power user should be able to check the JSON structure of a selected object.
I would like to display the JSON object in a computed text field but display/format it nicely so it is better human readable, something similar as in pretty print.
Is there any function I could use in SSJS that results in something similar so I can use display json nicely in computed text / editable fields?
Use stringify's third parameter "space":
JSON.stringify(yourObject, null, ' ');
space
A String or Number object that's used to insert white
space into the output JSON string for readability purposes. If this is
a Number, it indicates the number of space characters to use as white
space; this number is capped at 10 if it's larger than that. Values
less than 1 indicate that no space should be used. If this is a
String, the string (or the first 10 characters of the string, if it's
longer than that) is used as white space. If this parameter is not
provided (or is null), no white space is used.
As XPages doesn't support JSON.stringify yet you can include JSON's definition as SSJS resource and use it.
As Knut points out, you can certainly add json2.js to XPages; I've previously used an implementation as Marky Roden's post outlines. This is probably the "safest" way of doing so, from the SSJS side of things.
It does ignore the included fromJson and toJson SSJS methods provided out of the box in XPages. While imperfect, they are functional, especially with the inclusion of Tommy Valand's fix snippet. Be advised, using Tommy's fix does wrap responses to ensure a proper JS object can be parsed by shoving an Array into an object with a values property for the array; so no direct pulling of an Array only.
Additionally, I believe it would be useful to point out that a bean, providing a convenience method or two as wrappers to use either the com.ibm.commons.util.io.json methods to abstract the conversion method, or switching in something like Google GSON, might be more powerful and unified, based on your style of development.
Knut, Eric, I came so far myself already.
function prettyPrint(id) {
var ugly = dojo.byId(id).value;
var obj = $.parseJSON( "[" + ugly + "]" );
var pretty = JSON.stringify(obj, undefined, 4);
dojo.byId(id).innerHTML = pretty;
}
and I call it e.g.
var name = x$('#{id:input-currentObjectCollectionFiltered}').attr("name");
prettyPrint(name);
I tried to make use the x$ function but was not able to make the ID dynamic there e.g.
var ugly = x$('#{id:" + id + "}').val();
not sure why. would be nicer if I just would call prettyPrint('input-currentObjectCollectionFiltered'); and the function would figure it out.
Instead of dojo.byId(id).value I tried:
var ugly=$("#" + id).val();
but things returns and undefined object: I thought jquery would be smarter to work with dynamic id's.
anyway stringify works just fine.

AS3: Change TextField Text on multiple instances of the same MovieClip

I have a MovieClip called "number" in the library. I need to add multiple instances of that MovieClip to stage. Instances should be called number1, number2,number3...and each one needs to have different text inside it.
Is it possible to do this without code, just using flash interface tools? If not, could someone help me with coding that?
Thanks!
For an Class linked MyNumber containing a text field named output:
const N:int = 3; // 3 instances
const TEXTS:Array = ['text 1', 'text 2', 'text 3']; // 3 texts
var n:MyNumber;
for (var i:int = 0; i < N; i++) {
n = new MyNumber();
n.y = 50 * i;
n.output.text = TEXTS[i];
this.addChild(n);
}
You have to use code - at least a little bit.
In addition to #helloflash's answer, here is a simpler solution (with some caveats described below).
On your movieClip, make your text box dynamic, and give it an instance name of txt (or whatever you'd like). Then, put the following line of code on the first frame of your movieClips's timeline:
txt.text = this.name; //works if your text is a simple word with no spaces/puntuaction/symbols and doesn't match any actionscript keywords
This will set the text to whatever the instance name of each movieClip is. Will work great if you text is something simple like "Hello" or "Player1".
Now, if you're text is a number (or starts with one), or your text matches a keyword or already defined variable (like this/continue/function/break/stop/play etc), you'll need make it a bit more complicated, something like this:
txt.text = this.name.replace("$MC_","");
Then give your instance name in this format: $MC_stop, the code will strip out the $MC_ part and show the rest. so the text field would be "stop".
Now, if you want to include spaces, or most symbols (dollar sign, underscore and dash I think are the only supported ones), you'll have to add a replace for each one and create a place holder for that character.
So if your text was "This is my text", you should give it an instance name of `this_is_my_text" and this should be the code:
txt.text = this.name.replace("_"," "); //replace all underscores with space
Add as many replace statements for as many characters you need.
So, if you text was "1. This is my text!!!" - The instance name could be: $MC_1$dot_This_is_my_text$ex$ex$ex and the code:
txt.text = this.name.replace("$MC_","").replace("_"," ").replace("$dot",".").replace("$ex","!"); //you can keep chaining on as many replace statements as you need.
Of course, at this point you might as well just use full on code like #helloflash's answer. But if you text isn't that complicated, this may be a good solution for you.

Read Text File with AS3 and Tween Each Word from Small to Large out of the screen

I am looking for some advice on the best way to read in like 200k words and have them each tween from the center of the screen as small dots and tween up to the word filling the SWF then "fly through my head" not literally, but you probably get it...
What would be the best way in AS3 to go about this? I am fairly new to it.
Thanks!
Depending on the origin of your text, you may have to use regular expressions to get rid of punctuation and replace any amount of spaces by a set delimiter.
You could then use the split method of the String class to turn your text into an Array of words.
Each word can then be assigned to a Textfield. Since the Textfield is a DisplayObject, all the manipulations you are mentioning above become possible.
You may be able to streamline all this by creating a class that extends Textfield and defining various methods for the motions you want to implement.
You'll probably want to look at the Timer class and some tweening libraries
Pseudo Code
- Clean up String with regular expressions
-> expected result var cleanString:String = "word1;word2;...wordn";
- Turn String into Array
var words:Array = cleanString.split( ";" );
- Create a class that extends Textfield and define a manipulate() method
var tf:MyTextField = new MyTextField();
//this method could take parameters
//such as x, y, scale , time , delay , ease etc...
tf.manipulate();
- Create an Array( Vector ) of Textfields
loop thru words Array to return array of Textfields
var objects:Array = [ tf1, tf2 , etc...]
- Manipulate objects
loop thru objects Array to manipulate them
if you don't want to do it by hand (like PatrickS described) maybe this will help you ...
http://www.greensock.com/splittextfield/

as3 - detect urls in dynamic text and make them links

Anyone know of any good classes or functions that will do this? I've found some regexes but what I need is to pass the string to a method and have it return the same string, but with urls turned blue and turned into hyperlinks. Seems like a fairly common task, but I can't find anything.
EDIT - the following works for any link starting with http:
var myPattern:RegExp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
var str = text.replace(myPattern, "<font color='#04717D'><a target='_blank' href=\"$&\">$&</a></font>");
field.htmlText = str;
But it doesn't work for links that start with "www", because the href ends up looking like this:
www.google.com
Would love to know how to fix that.
I'm wary of making the existing regular expression/ replacement call any more complicated. With that in mind the most straightforward way of doing this is probably to write a second regular expression to correct any bad tags in the output from the first. I'd also add a 'g' to the end of your main regular expression so that it captures multiple URLs in the text
So, your main regular expression would now look like this:
var mainPattern:RegExp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig;
Your secondary regular expression will look something like this:
var secondaryPattern:RegExp = /\"www/g;
it should capture any links that don't start with "http:"
You then run both these expressions over your input string replacing as necessary:
var someText:String = "This is some text with a link in it www.stackoverflow.com and also another link http://www.stackoverflow.com/questions/5239966/as3-detect-urls-in-dynamic-text-and-make-them-links";
someText = someText.replace(mainPattern, "<a target='_blank' href=\"$&\">$&</a>");
someText = someText.replace(secondaryPattern, "\"http://www");