Replacing carriage returns and line feeds with newline characters using Regular Expressions

by Peter deHaan on December 16, 2008

in ActionScript, RegExp

The following example shows how you can use Regular Expressions to globally replace carriage returns and line feeds with the newline character (“\n”) in a string using Flex.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/16/replacing-carriage-returns-and-line-feeds-with-newline-characters-using-regular-expressions/ -->
<Application name="RegExp_crlf_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <Style>
        FxTextArea {
            textAlign: justify;
            textAlignLast: start;
        }
    </Style>

    <Script>
        <![CDATA[
            private var loremTEXT2:String;

            private function init():void {
                var crlf:String = String.fromCharCode(13, 10);
                var regEx:RegExp = new RegExp(crlf, "g");
                loremTEXT2 = loremTEXT.replace(regEx, "\\n");
            }
        ]]>
    </Script>

    <Declarations>
        <String id="loremTEXT" source="data/lorem.txt" />
        <String id="loremHTML" source="data/lorem.html" />
    </Declarations>

    <ApplicationControlBar dock="true">
        <FxButton label="TEXT" click="textArea.text = loremTEXT;" />
        <FxButton label="HTML" click="textArea.content = loremHTML;" />
        <Spacer width="20" />
        <FxButton label="TEXT (workaround)" click="textArea.text = loremTEXT2;" />
    </ApplicationControlBar>

    <FxTextArea id="textArea"
            fontSize="12"
            marginBottom="40"
            percentWidth="100"
            height="100%">
    </FxTextArea>

</Application>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.

{ 8 comments… read them below or add one }

1 speps December 17, 2008 at 2:40 pm
loremTEXT2 = loremTEXT.replace(/[\\r\\n]/g, "\\n");

Reply

2 Peter deHaan December 17, 2008 at 2:51 pm

speps,

Great tip, thanks!
That’s a much better solution than my temporary RegExp object.

Peter

Reply

3 Bounce February 5, 2009 at 10:40 am

Brilliant !

Just what i needed.

cheers man.
B.

Reply

4 Jason February 25, 2009 at 12:07 pm

Using a character class there means that you’ll replace either \r OR \n with \n, and thus you’ll still have double line returns in Flex. Remove the [] so it’s just
`loremTEXT.replace(/\r\n/gm, “\n”);`
To convert line endings from Windows style to Unix style.

Reply

5 Greg March 27, 2009 at 7:47 am

Nice,

Adding on to what Jason said, Mac OS 9 and lower computers use only carraige returns (“\r”) for a newline characters so you can even do it this way as well.

`loremTEXT.replace(/\r\n|\r/gm, “\n”);`

using the pipe, “|”, character will check for “\r\n” or “\r” and replace with “\n”.

Reply

6 nullnod April 8, 2009 at 9:06 am

use the + to denote you want one or more of either char in the charclass. it reads well too. sometimes the pipe is easy to overlook.

loremTEXT2 = loremTEXT.replace(/[\r\n]+/g, “\n”);

Reply

7 madhu May 18, 2009 at 6:31 am

I want to write a method which replaces all the places in a given string.

Ex:
string1=”d.o.t”
findString=”.o”
repString = “o”
string2 = MyReplaceMethod(string1,findString,repString)
then string2 must be “do.t”

repString = “”
string2 = MyReplaceMethod(string1,findString,repString)
then string2 must be “dot”

I tried to use regex but it is having many problems, it uses some characters as special characters which cannot be used in the find string, like .,[,\,* etc.
So how to replace using regex if find string contains these special characters

Without using regex, I was able to do
1. split the string with find string
2. append them back using the replace string in the middle

This works but i want a procedure which uses regex and works with any type of characters

Thank you
Madhu

Reply

8 madhu May 18, 2009 at 6:34 am

Ex:
string1=”d.o.t”
findString=”.o”
repString = “o”
string2 = MyReplaceMethod(string1,findString,repString)
then string2 must be “do.t”

findString=”.”
repString = “”
string2 = MyReplaceMethod(string1,findString,repString)
then string2 must be “dot”

examples corrected

Thank you
Madhu

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: