How to remove all line breaks from a string

0 votes
I have a text in a textarea and I read it out using the .value attribute.

Now I would like to remove all linebreaks (the character that is produced when you press Enter) from my text now using .replace with a regular expression, but how do I indicate a linebreak in a regex?

If that is not possible, is there another way?
Sep 18, 2020 in Java-Script by kartik
• 37,520 points
15,198 views

1 answer to this question.

0 votes

Hello @kartik,

Windows would be \r\n, but Linux just uses \n and Apple uses \r.

someText = someText.replace(/(\r\n|\n|\r)/gm, "");

That should remove all kinds of line breaks.

Hope it helps!!

Thank you!!

answered Sep 18, 2020 by Niroj
• 82,800 points

Related Questions In Java-Script

0 votes
1 answer

How to remove text from a div in JavaScript?

You can remove text from a <div> ...READ MORE

answered Mar 11, 2025 in Java-Script by Sanvi
1,280 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,800 points
1,782 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,800 points
14,454 views