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
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
Just tested this and it worked for me:
ta.text = ta.text.replace("\n",'');
var testString:String = "Hello\nWorld";
trace(testString);
testString = testString.replace("\n", '');
trace(testString);
Which yeilded the output:
Hello
World
HelloWorld
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:
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]+/,'');