In trying to find ideas for simple Flex projects that might make learning Flex a bit easier for beginners, I came across a web site that handles word counting and thought that would be a great beginner project. It turned out to be far easier than I’d initially anticipated, though admittedly, since it uses a regular expression, it’s not the easiest for a beginner to fully grasp. However, the regular expression used will hopefully illustrate the power of regular expressions and how useful they can be in Flex projects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute" viewSourceURL="srcview/index.html">
 
	<mx:Style>
		TextArea
		{
			cornerRadius: 5;
		}
		Application
		{
			backgroundColor: #FFFFFF;
		}
	</mx:Style>
 
	<mx:Script>
		<![CDATA[
			public function wordCount():void
			{
				if (countText.text == "")
				{
					//if there is not text, we'll set the total to 0
					totalWords.text = "0";
				}
				else
				{
					//to count the words, we'll split the string
					//into an array and get the length of that array
					var array:Array = countText.text.split(/\w+/);
					var total:int = array.length - 1;
					totalWords.text = total.toString();
				}
			}
		]]>
	</mx:Script>
 
	<mx:Form width="100%" height="100%">
		<mx:FormItem label="Text to Count">
			<mx:TextArea width="400" height="200"
				change="wordCount()" id="countText"/>
		</mx:FormItem>
		<mx:FormItem label="Total Words">
			<mx:Label id="totalWords" text="0"/>
		</mx:FormItem>
	</mx:Form>
 
</mx:Application>

Example: dm_word_counter.swf
Flex Project: dm_word_counter.zip

Leave a Reply