Remove new line in javascript code in string

vrugtehagel

I have a string with a line-break in the source code of a javascript file, as in:

var str = 'new
           line';

Now I want to delete that line-break in the code. I couldn't find anything on this, I kept getting stuff about \n and \r.

Thanks in advance!

EDIT (2021)

This question was asked a long, long time ago, and it's still being viewed relatively often, so let me elaborate on what I was trying to do and why this question is inherently flawed.

What I was trying to accomplish is simply to use syntax like the above (i.e. multi-line strings) and how I could accomplish that, as the above raises a SyntaxError.

However, the code above is just invalid JS. You cannot use code to fix a syntax error, you just can't make syntax errors in valid usable code.

The above can now be accomplished if we use backticks instead of single quotes to turn the string into a template literal:

var str = `new
           line`;

is totaly valid and would be identical to

var str = 'new\n           line';

As far as removing the newlines goes, I think the answers below address that issue adequately.
Jongware

If you do not know in advance whether the "new line" is \r or \n (in any combination), easiest is to remove both of them:

str = str.replace(/[\n\r]/g, '');

It does what you ask; you end up with newline. If you want to replace the new line characters with a single space, use

str = str.replace(/[\n\r]+/g, ' ');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Razor code creates new line inside of JavaScript string

From Dev

New line in JavaScript in the front of the string?

From Dev

How to remove new line from the string in awk?

From Dev

How to remove the last new line in javascript?

From Dev

Assembly code to print a new line string

From Dev

javascript setting a var with a new line break in string

From Dev

Remove new line character from C# String

From Dev

Remove Space at end of String but keep new line symbol

From Dev

How do I remove new line characters from a string?

From Dev

Bash string operations unwanted remove new line characters

From Dev

Remove new line from char string c++

From Dev

Bash string operations unwanted remove new line characters

From Dev

JavaScript - How to remove new line from textarea if no text exists before?

From Dev

JavaScript - How to remove new line from textarea if no text exists before?

From Dev

Trying to use a new line character in a regex is causing a "new line" in my javascript code

From Dev

Remove a string in every line

From Dev

To remove line based on string

From Dev

new line text causing error in javascript 'unterminated string literal'

From Dev

Append new line to string

From Dev

Interpreting a new line in string

From Dev

keystroke string with new line in it

From Dev

json decode remove the new line

From Dev

Tooltip CSS: Remove new line

From Dev

Javascript else on a new line

From Dev

Javascript not recognizing new line

From Dev

New line in JavaScript strings

From Dev

New line is not working in Javascript

From Dev

String s = new String("xyz"). How many objects has been made after this line of code execute?

From Dev

Remove a href from code line

Related Related

HotTag

Archive