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

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 thoughts on “Replacing carriage returns and line feeds with newline characters using Regular Expressions

  1. 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.

  2. 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”.

  3. 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”);

  4. 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

  5. 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

Comments are closed.