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″.
<?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.
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.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
8 Responses to Replacing carriage returns and line feeds with newline characters using Regular Expressions
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


speps,
Great tip, thanks!
That’s a much better solution than my temporary RegExp object.
Peter
Brilliant !
Just what i needed.
cheers man.
B.
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.
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”.
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”);
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
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