Remove all but the first word from a sentence - actionscript-3

I need to find a way to take a sentence and remove all its words besides the first.
If the sentence is "Hi my name is dingo"
I need to get only the word "Hi"

var sentence : String = "Hi my name is dingo"
var words : Array = sentence.split( " " );
var firstWord : String = words[ 0 ];
trace( firstWord ) // outputs "Hi"
Obviously this only works for simple sentences without punctuation. If you need more complex word parsing you can use regexp:
var pattern : RegExp = new RegExp( "\\b.(\\w*).\\b",'gi' );
var words : Array = sentence.match( pattern );

Related

Partial replace in docs what matches only and preserve formatting

Let's assume that we have first paragraph in our google document:
Wo1rd word so2me word he3re last.
We need to search and replace some parts of text but it must be highlighted in editions history just like we changed only that parts and we must not loose our format (bold, italic, color etc).
What i have/understood for that moment: capturing groups didn't work in replaceText() as described in documentation. We can use pure js replace(), but it can be used only for strings. Our google document is array of objects, not strings. So i did a lot of tries and stopped at that code, attached in this message later.
Can't beat: how i can replace only part of what i've found. Capturing groups is very powerful and suitable instrument, but i can't use it for replacement. They didn't work or i can replace whole paragraph, that is unacceptable because of editions history will show full paragraph replace and paragraphs will lose formatting. What if what we searching will be in each and every paragraph, but only one letter must be changed? We will see full document replacement in history and it will be hard to find what really changed.
My first idea was to compare strings, that replace() gives to me with contents of paragraph then compare symbol after symbol and replace what is different, but i understand, that it will work only if we are sure that only one letter changed. But what if replace will delete/add some words, how it can be synced? It will be a lot bigger problem.
All topics that i've found and read triple times didn't helped and didn't moved me from the dead point.
So, is there any ideas how to beat that problem?
function RegExp_test() {
var docParagraphs = DocumentApp.getActiveDocument().getBody().getParagraphs();
var i = 0, text0, text1, test1, re, rt, count;
// equivalent of .asText() ???
text0 = docParagraphs[i].editAsText(); // obj
// equivalent of .editAsText().getText(), .asText().getText()
text1 = docParagraphs[i].getText(); // str
if (text1 !== '') {
re = new RegExp(/(?:([Ww]o)\d(rd))|(?:([Ss]o)\d(me))|(?:([Hh]e)\d(re))/g); // v1
// re = new RegExp(/(?:([Ww]o)\d(rd))/); // v2
count = (text1.match(re) || []).length; // re v1: 7, re v2: 3
if (count) {
test1 = text1.match(re); // v1: ["Wo1rd", "Wo", "rd", , , , , ]
// for (var j = 0; j < count; j++) {
// test1 = text1.match(re)[j];
// }
text0.replaceText("(?:([Ww]o)\\d(rd))", '\1-A-\2'); // GAS func
// #1: \1, \2 etc - didn't work: " -A- word so2me word he3re last."
test1 = text0.getText();
// js func, text2 OK: "Wo1rd word so-B-me word he3re last.", just in memory now
text1 = text1.replace(/(?:([Ss]o)\d(me))/, '$1-B-$2'); // working with str, not obj
// rt OK: "Wo1rd word so-B-me word he-C-re last."
rt = text1.replace(/(?:([Hh]e)\d(re))/, '$1-C-$2');
// #2: we used capturing groups ok, but replaced whole line and lost all formatting
text0.replaceText(".*", rt);
test1 = text0.getText();
}
}
Logger.log('Test finished')
}
Found a solution. It's a primitive enough but it can be a base for a more complex procedure that can fix all occurrences of capture groups, detect them, mix them etc. If someone wants to improve that - you are welcome!
function replaceTextCG(text0, re, to) {
var res, pos_f, pos_l;
var matches = text0.getText().match(re);
var count = (matches || []).length;
to = to.replace(/(\$\d+)/g, ',$1,').replace(/^,/, '').replace(/,$/, '').split(",");
for (var i = 0; i < count; i++) {
res = re.exec(text0.getText())
for (var j = 1; j < res.length - 1; j++) {
pos_f = res.index + res[j].length;
pos_l = re.lastIndex - res[j + 1].length - 1;
text0.deleteText(pos_f, pos_l);
text0.insertText(pos_f, to[1]);
}
}
return count;
}
function RegExp_test() {
var docParagraphs = DocumentApp.getActiveDocument().getBody().getParagraphs();
var i = 0, text0, count;
// equivalent of .asText() ???
text0 = docParagraphs[i].editAsText(); // obj
if (text0.getText() !== '') {
count = replaceTextCG(text0, /(?:([Ww]o)\d(rd))/g, '$1A$2');
count = replaceTextCG(text0, /(?:([Ss]o)\d(me))/g, '$1B$2');
count = replaceTextCG(text0, /(?:([Hh]e)\d(re))/g, '$1C$2');
}
Logger.log('Test finished')
}

Separate strings in a string with Actionscripts3

I'm trying to separate two part of a string, one is Title one is Value, RegExp is confused me. I need your help to solve this thanks
var pattern2:RegExp = new RegExp("TZ_NUM_ANSWER:Telegram code([0-9.-]+)");//TZ_NUM_ANSWER:Telegram code 32263
var data2:Object = pattern2.exec(response);
if (data2 != null && data2[1] != null)
{
var value2:Number = parseFloat(data2[1]);
trace("TZ_NUM_ANSWER " + value2);
txt_BUY1.text = String(value2);
}
Output:
TZ_NUM_ANSWER:Telegram code 32263
It must be:
"TZ_NUM_ANSWER:" "Telegram code 32263"
The result of split is an Array you can access to Array indexes and assign them to a variable.
var STR1:String = "TZ_NUM_ANSWER:Telegram code 32263";
var STR2:String;
var STR3:String;
trace(STR1.split(":"));
STR2 = STR1.split(":")[0];
STR3 = STR1.split(":")[1];
trace (STR2);
trace (STR3);
Result:
TZ_NUM_ANSWER
Telegram code 32263
Don't use RegEx for simple stuff. All you need is basic string methods:
response.split(":");

Add a "/" between each word in a String

I've got this string :
var str:String = mySharedObject.data.theDate;
where mySharedObject.data.theDate has some words (not always the same words has it depends on which button the user clicked).
So mySharedObject.data.theDate = "words words words".
Is it possible to add a "/" between each word ? (without knowing which words are in mySharedObject.data.theDate).
In order to have:
mySharedObject.data.theDate = "words/words/words".
Edit : You can replace " " with "/" in your string, this will split string with " " separator and then join with "/"
mySharedObject.data.theDate= mySharedObject.data.theDate.split(" ").join("/")
You can also do that using String.replace() with a little regular expression which will replace all spaces (notice here the g (global) flag to replace all instances), like this :
var s:String = 'word word word';
trace(s.replace(/\s/g, '/')); // gives : word/word/word
And for more about regular expressions take a look here.
Hope that can help.

Split textfield value in 2 parts and put each part in a var

I have a textfield, that has a text value: France Paris. Now I need to know how I can take that string, cut it in 2 parts (France and Paris) and put those two parts in a var. So:
<textfield id="field1" text="France Paris">
and somewhere i should get
var1 = France;
var2 = Paris;
I know there is a split string command, but I'm not familiar with any of this stuff.
For this you could use the split() method on your String. This will split your String and put the values in an Array. You can use it like this:
var yourString:String = "France Paris";
var splitString:Array = yourString.split(" ");
var firstWord:String = splitString[0];
var secondWord:String = splitString[1];
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#split()

delete leading characters

I'm a noob to actionscript so this should be easy:
How do I delete leading characters from a string? I have a string that contains (at times) both numeric & non-numeric characters. If I want to delete all the leading 9's, how would I do that?
var testVar:String = '999998gjek74k';
I want the testVar to be 'gjek74k'.
So far, I have (though not working):
var testVar:String = '999998gjek74k';
testVar.replace(/^0/g, "");
.replace doesn't modify the string. It returns the replaced string.
testVar = testVar.replace(/^\d+/, '');
(Also the pattern /^0/g is wrong, as commented by #santa).
Assuming you are testing the variables and not multiple lines:
private var testVar = testVar.replace(/^\d*(.+)$/,"$1");