Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to replace all carriage returns in a string via .replace?

Is it possible to replace all carriage returns in a string with the .replace function? I've found quite a few complex functions to do it, but was wondering if it could be simplified with just a regex through .replace?

Thanks!

like image 832
Mark Avatar asked Sep 07 '11 02:09

Mark


People also ask

How do I replace a carriage return in a string?

In the Find box hold down the Alt key and type 0 1 0 for the line feed and Alt 0 1 3 for the carriage return. They can now be replaced with whatever you want.

Does string replace replacing all occurrences?

3.1 The difference between replaceAll() and replace() If search argument is a string, replaceAll() replaces all occurrences of search with replaceWith , while replace() only the first occurence. If search argument is a non-global regular expression, then replaceAll() throws a TypeError exception.

How do you replace all values in a string?

replaceAll() The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. The original string is left unchanged.

How do you replace a carriage return in Java?

replaceAll("\\n", ""); s = s. replaceAll("\\r", ""); But this will remove all newlines. Note the double \ 's: so that the string that is passed to the regular expression parser is \n .

How to replace (new line) and (Carraige return) at the same time?

Both (new line) and (carraige return) create a new line. To replace all instances of both at the same time: Note that you might want to replace them with a single space rather than nothing. Show activity on this post.

How to replace text with carriage return in Excel?

We can also replace text with carriage return by using Keyboard Shortcuts. First of all, select all the cells. I selected here cells B5:C9. Press CTRL + H to have Find & Replace box. Input the Text to be replaced in Find what. Here, I wrote Manchester United to replace. Next, press CTRL + J in Replace with as we want carriage return.

How to replace text Manchester United in C5 cell with carriage return?

Here, I used the SUBSTITUTE function to Replace the text Manchester United in the C5 cell with a carriage return by using the CHAR function. Now, select cell D5 then click on Wrap Text. Then, we will have the text replaced with a carriage return. Use Fill Handle to AutoFill the rests. You can modify the Row Heights according to your choice. 4.

How do I replace a string with a regex in JavaScript?

str = str.replace (/[&r&]/gm,'newChar'); By default, Javascript replace () replaces the first occurance. The way around it is to set the first parameters as a regex.


2 Answers

Both \n (new line) and \r (carraige return) create a new line. To replace all instances of both at the same time:

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

Note that you might want to replace them with a single space rather than nothing.

like image 94
RobG Avatar answered Oct 05 '22 09:10

RobG


Here how to do it

str = str.replace(/\r/gm,'newChar');

By default, Javascript replace() replaces the first occurance. The way around it is to set the first parameters as a regex.

like image 23
David Laberge Avatar answered Oct 05 '22 11:10

David Laberge