473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Regular Expression help: multi-line mode

I wish to remove the last newline (if any) from a string.
This is where I started:

var text = "hello\n";
text = text.replace( /^(.*)\n?$/, "$1" );

This seems to work, but not if the test has embedded newlines:
text = "hello\ngood-bye\n";

I've tried a few variations but no luck so far.
(I worked around the problem by testing if the
last character == '\n' and if so use text.slice(0,-1)
to remove it.)

Can someone provide me the proper regex for this?
Thanks!

-Wayne
Dec 29 '07 #1
10 1299
Wayne wrote:
I wish to remove the last newline (if any) from a string.
The do
string = string.replace(/\n$/, '');
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 29 '07 #2
Wayne said:
Can someone provide me the proper regex for this?
Thanks!

-Wayne
Wayne,

I extrapolated it out to a separate call, and it worked for me.

alert(str.replace("\n"), "$1");

~A!

--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 29 '07 #3
Martin Honnen wrote on 29 dec 2007 in comp.lang.javascript:
Wayne wrote:
>I wish to remove the last newline (if any) from a string.

The do
string = string.replace(/\n$/, '');
Non regex variation:

var temp = str.split('\n');
if (!temp.pop()) str = temp.join('\n');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 29 '07 #4
Anthony Levensalor wrote on 29 dec 2007 in comp.lang.javascript:
Wayne said:
>Can someone provide me the proper regex for this?
Thanks!

-Wayne
Wayne,

I extrapolated it out to a separate call, and it worked for me.

alert(str.replace("\n"), "$1");
I am sure you did not test that!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 29 '07 #5
Evertjan. said:
[snip]
I am sure you did not test that!
I did, and it removed the newlines. Looking at it right now, though, I
see an obvious parens issue, how the hell did that even happen? Well,
even with the parens screwed up, it didn't toss any errors, and it
pulled out the newlines I stuck into a string.

--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 29 '07 #6
Anthony Levensalor wrote on 29 dec 2007 in comp.lang.javascript:
Evertjan. said:
[snip]
>I am sure you did not test that!
I did, and it removed the newlines. Looking at it right now, though, I
see an obvious parens issue, how the hell did that even happen? Well,
even with the parens screwed up, it didn't toss any errors, and it
pulled out the newlines I stuck into a string.
But that was not the OQ.

Then even it did only work,
because of the strange compatibility of alert().

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 29 '07 #7
Martin Honnen wrote:
Wayne wrote:
>I wish to remove the last newline (if any) from a string.

The do
string = string.replace(/\n$/, '');

So simple! That's what I get for working through the night,
on a weekend to boot! Thanks!

-Wayne
Dec 29 '07 #8
Evertjan. said:
[snip]
Then even it did only work,
because of the strange compatibility of alert().
I am 100% certain that's the only way it could have possibly worked with
so obvious a syntax error in it. I've been awake for three days and
haven't really left my desk, so if I missed a parenthesis issue, then I
missed it. Oh friggin' well.

~A!

--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 29 '07 #9
Evertjan. wrote:
Martin Honnen wrote on 29 dec 2007 in comp.lang.javascript:
>Wayne wrote:
>>I wish to remove the last newline (if any) from a string.
The do
string = string.replace(/\n$/, '');

Non regex variation:

var temp = str.split('\n');
if (!temp.pop()) str = temp.join('\n');

Thanks! I know there must be a million ways,
but I am looking for one that is efficient.
It seems to me this would be slower (and take more
memory) than Martin's RegExp.

-Wayne
Dec 29 '07 #10
Wayne wrote on 29 dec 2007 in comp.lang.javascript:
Evertjan. wrote:
>Martin Honnen wrote on 29 dec 2007 in comp.lang.javascript:
>>Wayne wrote:
I wish to remove the last newline (if any) from a string.
The do
string = string.replace(/\n$/, '');

Non regex variation:

var temp = str.split('\n');
if (!temp.pop()) str = temp.join('\n');

Thanks! I know there must be a million ways,
but I am looking for one that is efficient.
It seems to me this would be slower (and take more
memory) than Martin's RegExp.
I doubt that, in the case of the absence of the final \n,
as in the regex case the string is still duplicated twice,
once to an internal temporary location and then back
to the variable's string space. It is nearly as bad as string
concatenation.

So if speed were at a premium [which I doubt to be the case]
better do this:

if (/\n$/.test(string))
string = string.replace(/\n$/, '');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 29 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
3
by: Bryan | last post by:
Hi All: I'm trying to find the right Regexp string to remove empty SPAN tags from an HTML string. Say I have a string like so, and I want to remove the empty span tags: <span>This is my...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
3
by: Ryan Taylor | last post by:
Hello. I am trying to create a regular expression that will let me know if a string has the following criteria. Order does not matter in the string, but when building a regular expression it...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
0
by: Duncan Grisby | last post by:
Hi, I have encountered a problem with the re module. I have a multi-threaded program that does lots of regular expression searching, with some relatively complex regular expressions....
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
8
by: Michael Primeaux | last post by:
I'm somewhat new to regular expressions and am in need of assistance. I'd like to match on any OBJECT, EMBED, or APPLET element nested inside an <HTML> element. Would someone point me in the right...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.