views:

44

answers:

2

What is the regular expression to find the first newline (\n) in a text (used to find and delete the newline)? I'm using the regular expression in ActionScript and tried

ta.text = ta.text.replace(/\n*/,'') 

but it doesn't seem to work

Thanks

A: 

Just tested this and it worked for me:

ta.text = ta.text.replace("\n",'');


My actual code was (cut and pasted):

var testString:String = "Hello\nWorld";
trace(testString);
testString = testString.replace("\n", '');
trace(testString);

Which yeilded the output:

Hello
World
HelloWorld


Alternatively, you can define a pattern along the lines of what you were attempting:

var pattern:RegExp = /AB\*C/;

And that works as well. The modified code would become:

var pattern:RegExp = /\n/;
var testString:String = "Hello\nWorld";
trace(testString);
testString = testString.replace(pattern, '');
trace(testString);

Note that the code above only replaces the first instance of a newline character (as you requested). Doing more would require either a recursive call to the replace function or a more sophisticated RegExp.

I hope that helps in some way,

--gMale

EDIT: given the comment discussion below, try working with one of these events, instead:

  • change Dispatched when text in the TextArea control changes through user input.
  • dataChange Dispatched when the data property changes.
  • textInput Dispatched when the user types, deletes, or pastes text into the control.
gmale
Thanks, the code definitely works for a string. But after doing some testing, I've found that it does not work for a textArea in flex and that's my problem. Other patterns will work but not one for newline. Same code except testTextArea.text.replace instead of testString.replace. Does anyone have any idea why?
Steven
@Steven: That's odd... are you reassigning the result back to testTextArea.text?
Cameron
@Steven: On Windows, TextArea.text will have CRLF line breaks. The line of code at the end of my answer should work regardless of the line break style.
Jan Goyvaerts
try \r instead of \n and see what that does. If it works, you can make a more sophisticated regex that will work on any platform.
gmale
Thanks everyone, I think I've pinpointed the problem and it doesn't have to do with the reg exp. I'm using flex 3.5 and was calling the replace function within the updateComplete event for the textArea. If I trace the text in the textArea it displays the text correctly, and other search/replace calls work fine but not ones for a newline (search/replace won't detect any newlines in the textArea). I think it has something to do with updateComplete and the flex internal workings but not sure what exactly.
Steven
@Steven: Let me understand what you're saying... does the search/replace work for newlines that are **not** in the text area? If that's the case, then I agree that it seems to point toward an event handling issue. Is there a reason you're using FlexEvent.UPDATE_COMPLETE? You may be able to work with one of the TextArea events instead, like *change, dataChange, or textInput*. I'll update my answer with links.
gmale
I meant that the search/replace function works for other types of searches such as a basic search for "cow" but does not work for newline (in update_complete, that is). I found a workaround to not use the update_complete event. Thanks
Steven
+1  A: 

You're using the regular expression \n* which matches the first occurrence of zero (!) or more line feed characters. The first match of this regex is thus always at the very start of the string. If the string starts with line feed characters, those will be matched. If the string starts with something else, the zero-length string at the start of the regex will be matched.

Use \n to match the first line feed character. Use \n+ to match the fist sequence of line feed characters. Use [\r\n]+ to match the first sequence of line breaks, regardless of the line break style used (LF only, CRLF, etc.). Use \r?\n to match a single line break as either LF only or CRLF.

In your ActionScript code, use two slashes to delimit the regex you want to use:

ta.text = ta.text.replace(/[\r\n]+/,'');
Jan Goyvaerts