Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code below will remove all newlines.
I would like to know a regular expression that can remove newlines without periods.

Happy new years happy\n (<= remove)
new years happy new\n (<= remove)
years.\n (<== don't remove)
happy new years happy\n (<= remove)
new years.\n (<= don't remove)

What I have tried:

var text = str.replace(/(\r\n|\n|\r)/gm, "");
Posted
Updated 1-Jan-23 7:25am
Comments
0x01AA 1-Jan-23 8:52am    
Maybe something like: str.replace(/(?<!\.)\n/g, " ");

[Edit]
Keep in mind I used \n as a general placeholder, you most probably need to replace that with \n|\r\n|\r ;)
0x01AA 1-Jan-23 12:55pm    
Does it work or not? If not, what is wrong?
Chopin2001 1-Jan-23 13:10pm    
It works!
I've tested it in various ways now.
Thanks so much.
Happy new years!
새해 복 많이 받아요. ^^
0x01AA 1-Jan-23 13:24pm    
Happy new year you too ;)

1 solution

Try this:
str.replace(/(?<!\.)\n/g, " ");

To be honest, that solution I worked out by the help of 'ChatGPT' and cross checked it with an online regex tool ;)
 
Share this answer
 
v2
Comments
Chopin2001 1-Jan-23 14:28pm    
Can you tell me online regex tool?
Graeme_Grant 1-Jan-23 19:05pm    
regex101: build, test, and debug regex[^] is one of my favourites. 😊
Chopin2001 1-Jan-23 19:44pm    
Tried it out, it's a very useful tool. thank you so much.
0x01AA 2-Jan-23 12:48pm    
Same here ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900