Thursday, October 7, 2010

Create a HTML-Tag parser using AS3

Today we will learn how to create a flash application that will parse html tags.

This feature is often used in forums and chats: users aren't allowed to use real html tags in their messages, but they are welcome to use such tags like [b], [i] and [u] (and the closing versions of the same tags, ex. [/b], etc.)

Here is my little utility:


As you can see, this feature will turn the usual < and > html tags into the text versions of the same symbols (&lt; and &gt;). The flash will generate a group of 4 character in the place of these tags, but when parsed in html - the user will see them as < and >, they won't be read as if they were real html tags.

The second feature is that we can change specific tags in [ ] to real html tags.

I have two text fields on my stage, called inputText and outputText, as well as a button, called parseBtn.

Here's my code:

parseBtn.addEventListener(MouseEvent.CLICK, parse);

function parse(MouseEvent):void{
// Remove all html tags
outputText.text = inputText.text.replace(/</g,"&lt;");
outputText.text = outputText.text.replace(/>/g,"&gt;");
// Parse b, i and u tags
outputText.text = outputText.text.replace(/\[((\/)?(b|i|u))\]/gi,"<$1>");
}

As you can see, I use the replace(); method. And not just once, but three times. The first replace(); line will turn all <'s into [, the second will turn > into ]. Why did I use the Regular Expression format instead of simple strings as the first parameter? Because I want to add the "g" tag, which means check more than once, until there are no more matches.

The final replace(); line is the most complex one. It will check if a tag is in [ ] and if the inside of the tag is either b, i or u. Here's the RegExp taken appart:

/   \[   ( (\/) ? (b|i|u) )   \]   / gi

The 2 / symbols in the beginning and in the end of the RegExp define the beginning and the end. I put the g flag for global (to check more than once) and the i flag (case-insensitive). The \[ and \] mean the [ and ] tags (I need the \'s in front of them, because otherwise they will be read literally in regular expression (not as a piece of text)).

In the middle, I have one () group. I can later call it by using $1 in the second parameter of replace() method. This major group contains 2 others:
(the "/" symbol can or can not be here (thanks to the ?)) + ("b" or "i" or "u")

I hope this was helpful. Thanks for reading!

11 comments:

mytapworld said...

The best simple minimalisttapworld

curtains accept to be the simple chrome ones,Taps

Australia
whether that's for the kitchen as acclimated as a kitchen bore curtains

or acclimated as a ablution curtains in the bath sinks.Kitchen taps With these avant-garde

minimalist curtains the articles consistently analysis the articles afore its alien to

the customers.Shower

heads
You can calmly acquisition superior minimalist curtains for kitchens and bath

throughout the online market.source:http://www.tapworld.com.au/

mytapworld said...

The best simple minimalisttapworld

curtains accept to be the simple chrome ones,Taps

Australia
whether that's for the kitchen as acclimated as a kitchen bore curtains

or acclimated as a ablution curtains in the bath sinks.Kitchen taps With these avant-garde

minimalist curtains the articles consistently analysis the articles afore its alien to

the customers.Shower

heads
You can calmly acquisition superior minimalist curtains for kitchens and bath

throughout the online market.source:http://www.tapworld.com.au/

robinet sale said...

J'ai ensuite place http://www.robinetsale.com/ et utiliser l'outil interactif dans leur onglet de robinetterie cuisine de vie, trouver mon rêve de diriger. robinet LED et ils ont un joli petit 5 problèmes, pour vous aider à choisir le plus adapté à votre Robinetterie Vasque .

robinet sale said...

J'ai ensuite place http://www.robinetsale.com/ et utiliser l'outil interactif dans leur onglet de robinetterie cuisine de vie, trouver mon rêve de diriger. robinet LED et ils ont un joli petit 5 problèmes, pour vous aider à choisir le plus adapté à votre Robinetterie Vasque .

Kate moss said...

During installation, it is important that the washbasin LED Taps are mounted. Proper plumbing is important, because the installation is not an easy task and could a mess sometimes. Whenever possible, avoid Do-It-Yourself Plumbing when remodeling a Waterfall Taps, unless you're a plumber, of course r. Plumbing is a complicated job when you do not have a clue how. Thus, it is much better if you let the experts! When shopping for Antique Taps, go to online stores can offer you not only the ease of purchase, but a lot more variety and great Taps Sale .However, make sure the online store where you plan to buy your plumbing fixtures is one of Antique Kitchen Taps.

sherry jiang said...

A man of taste quality of life is high, especially bathroom design often can reflect a person's quality of life.Waterfall taps are a stylish way to make a statement in your bathroom, and are becoming increasingly popular and very affordable. http://www.plumpinguk.co.uk/kitchen-taps/wall-mounted-taps.html.It will make your life more refineddand we have created a lot of artistic products of"stylish personality and quality life".

Cheap Tap said...

In fact, there are a variety of “ waterfall taps”, they are in many different designs, without exception, is the same as the water just poured out. It can be divided into washbasin, bathtub, shower and so on types of models, plus a touch of electonic switches, can control the temperature and the amount of water. Lightly touch, light and water is pouring down from the tap in the form of a waterfall. Elegant and gorgeous. Almost had a huge change in the high-tech and creative sense to bring human life. It is really looking forward to use the http://www.cheaptap.co.uk/waterfall-taps-c-36.html.

Zhu Y said...

Our pneumatic factory was founded in 2000,CHECK HERE. In decades of serving China pneumatic industry, we have built our reputation on product knowledge, world class suppliers, application support, and value add services. Our commitment to our employees, customers and vendors is the core of our business beliefs. In our factory, there are more than 80 front-line workers, 3000 squares working shop.Our main products are air Pneumatic Cylinder, solenoid valve and also a lot of pneumatic fittings like Air Gun.

Zhu Y said...

Our pneumatic factory was founded in 2000,CHECK HERE. In decades of serving China pneumatic industry,

we have built our reputation on product knowledge, world class suppliers, application support, and value add services. Our commitment to

our employees, customers and vendors is the core of our business beliefs. In our factory, there are more than 80 front-line workers, 3000

squares working shop.Our main products are air Pneumatic Cylinder, solenoid valve and also a lot of pneumatic fittings

like Air Gun.

dc Z said...

Imagine a beautiful room that draws gasps of appreciation when people enter the room. If you are thinking of your living room, adjust your perspective and take a mental walk to your shower.http://www.findtaps.co.uk/36-shower-taps

Amy Carry said...

Which kind of faucets can be called http://www.faucetsmarket.com/waterfall-faucets-c-34.html ? Waterfall is a rare natural landscape in the city, for this rare Bathtub faucet , designers add waterfall elements in to Faucet parts and let your home will be able to get more closer to the nature.

Post a Comment