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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

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

  1. speps says:
    loremTEXT2 = loremTEXT.replace(/[\\r\\n]/g, "\\n");
    
  2. Peter deHaan says:

    speps,

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

    Peter

  3. Bounce says:

    Brilliant !

    Just what i needed.

    cheers man.
    B.

  4. Jason says:

    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.

  5. Greg says:

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

  6. nullnod says:

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

  7. madhu says:

    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

  8. madhu says:

    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

Leave a Reply

Your email address will not be published.

You may 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