# How to tell the difference between \\n in a string and a newline?

**URL:** <https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922>\
**Category:** General\
**Tags:** function-node\
**Created:** [25 April 2023 14:09 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922 "2023-04-25T14:09:00Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [25 April 2023 14:09 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/1 "2023-04-25T14:09:00Z")

</div>

Hi,

I have to separate a character string line by line (to process them individually afterwards):

```auto
/*
* My name
*/
#include <asf.h>
#include "language.h"

//defaut
static const char lg_clic1sec_cancel_fr[] = {"Clic 1sec:\nAnnule"};
static const char lg_clic1sec_cancel_en[] = {"Click 1sec:\nCancel"};
const char * lg_clic1sec_cancel[] = {lg_clic1sec_cancel_fr, lg_clic1sec_cancel_en};

```

. I use `file.split('\n')`

The problem is that in my character string there is written `\n` in all letters, suddenly the `file.split` cuts the text at this place!

```auto
[{"id":"2b8e344e06f27e38","type":"function","z":"378c07b4.3c0cd8","name":"test","func":"const file = \n\n`\n/*\n* My name\n*/\n\n#include <asf.h>\n\n#include \"language.h\"\n\n//defaut\nstatic const char lg_clic1sec_cancel_fr[] = {\"Clic 1sec:\\nAnnule\"};\nstatic const char lg_clic1sec_cancel_en[] = {\"Click 1sec:\\nCancel\"};\nconst char * lg_clic1sec_cancel[] = {lg_clic1sec_cancel_fr, lg_clic1sec_cancel_en};\t\n\n`\n\nlet lines = file.split('\\n')\n\nnode.warn(lines)//affiche lignes par ligne originales\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2370,"y":780,"wires":[["91883f67f4e39991"]]},{"id":"fb99e1973b640a89","type":"inject","z":"378c07b4.3c0cd8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2220,"y":780,"wires":[["2b8e344e06f27e38"]]}]

```

How can I tell the difference between a line break and a written `\n` ?  
or how to separate each line without taking into account `\n` ?

Thanks

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [25 April 2023 14:14 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/2 "2023-04-25T14:14:20Z")

</div>

That seems to be C code?

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [25 April 2023 14:21 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/3 "2023-04-25T14:21:26Z")

</div>

yes that's right, I have to insert lines in addition to this existing code with Node Red. That's why I have to separate the lines.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [25 April 2023 14:27 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/4 "2023-04-25T14:27:32Z")

</div>

Can't you escape the \n with \ (or is it /) before it? (Brain fog - man flu 😂)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [25 April 2023 14:35 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/5 "2023-04-25T14:35:40Z")

</div>

What does the data look like as it comes into Node-red? Use a debug node and show us it's output.

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [25 April 2023 14:40 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/6 "2023-04-25T14:40:39Z")

</div>

I copy and paste text in node red by putting it in the file constant  
[EDIT]  
Damn, I just saw that if I do `node.warn(file)` the lines are already skipped by Node red as soon as there is a `\n` in the text!  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8b7c20e2522a352efbfeb18c31f5f7aa9b06eec.png)

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [25 April 2023 14:42 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/7 "2023-04-25T14:42:25Z")

</div>

I tried `file.split('/')` and here it is:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a33a6ceaab6b410df389707d9c70e781c02bcd1.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [25 April 2023 14:46 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/8 "2023-04-25T14:46:26Z")

</div>

Oh that code _is_ your data. I misunderstood.

You could do something like this

```auto
const foo = file.replace( ":\n", ":::")
let lines = foo.split('\n')

```

At some point in the flow you will have to change `:::` back to `:\n`. Not sure how that will work.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 April 2023 15:05 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/9 "2023-04-25T15:05:54Z")

</div>

Try

```auto
let lines = file.split(/(?<!:)\n/)

```

But it would be best to correct the code and remove non valid property characters from your property names, then you would not have to patch the issue.

Just for info

```auto
const foo = file.replace( ":\n", ":::")

```

will only replace first occurance.  
it should be

```auto
const foo = file.replace( /:\n/g, ":::")

```

for multiple replaces.

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [25 April 2023 15:16 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/10 "2023-04-25T15:16:49Z")

</div>

Thanks, @jbudd looks like it doesn't resolve all the \n :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d9adfaad77f985dacb2fdbe18908d134d302ca50.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [25 April 2023 15:19 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/11 "2023-04-25T15:19:34Z")

</div>

@E1cid's answer is much better than mine.

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [25 April 2023 15:33 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/12 "2023-04-25T15:33:57Z")

</div>

Great @E1cid , @jbudd guys, I think I'm going to do a mix of the 2 to get by  
THANKS

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [26 April 2023 09:10 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/13 "2023-04-26T09:10:11Z")

</div>

One last query. In the other direction: how to transform the line break into a `\n` character?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03574289846fb6d61c4fbe6214bfabfa281c1550.png)  
_the original text after the_ ` let lines = file.split(/(?<!:)\n/)`

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea57ba623ec35fd12dc2e63d4f08fa3802c65c92.png)  
_the new File modified and saved with a "write file node"_

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 April 2023 09:23 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/14 "2023-04-26T09:23:13Z")

</div>

I would think that the \n would need escaping when joining `\\n`, but my example should not have split them in first place, confused.

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [26 April 2023 09:55 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/15 "2023-04-26T09:55:47Z")

</div>

`let lines = file.split(/(?<!:)\n/)`  
=\> I add `\` in front of n , right ?  
` let lines = file.split(/(?<!:)\\n/)`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/caa686719398056ce77a4c59ad1a232ea9f666ad.png)  
the result is that I only have one line. I can't process my text behind line by line.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 April 2023 10:09 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/16 "2023-04-26T10:09:47Z")

</div>

No, you need to add the extra `\` then split.

```auto
let file = 

`
/*
* My name
*/

#include <asf.h>

#include "language.h"

//defaut
static const char lg_clic1sec_cancel_fr[] = {"Clic 1sec:\nAnnule"};
static const char lg_clic1sec_cancel_en[] = {"Click 1sec:\nCancel"};
const char * lg_clic1sec_cancel[] = {lg_clic1sec_cancel_fr, lg_clic1sec_cancel_en};	

`
file = file.replace(/:\n/g, ":\\n");
let lines = file.split(/(?<!\\)\n/)

node.warn(lines)//affiche lignes par ligne originales

```

Or  
after joining

```auto
const file = 

`
/*
* My name
*/

#include <asf.h>

#include "language.h"

//defaut
static const char lg_clic1sec_cancel_fr[] = {"Clic 1sec:\nAnnule"};
static const char lg_clic1sec_cancel_en[] = {"Click 1sec:\nCancel"};
const char * lg_clic1sec_cancel[] = {lg_clic1sec_cancel_fr, lg_clic1sec_cancel_en};	

`

let lines = file.split(/(?<!:)\n/)

node.warn(lines)//affiche lignes par ligne originales
lines = lines.join("\n");
lines = lines.replace(/:\n/g, ":\\n");
node.warn(lines);

```

Again i think you are creating a nightmare for yourself in the future, all these design errors need fixing for your future ease.

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [26 April 2023 10:41 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/17 "2023-04-26T10:41:35Z")

</div>

wow that's awesome that's all I ask you're amazing 🤩

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/404591483e38884640443c49427bd61585c29ead.png)

> [@E1cid](#):
>
> Again i think you are creating a nightmare for yourself in the future, all these design errors need fixing for your future ease.

I totally agree with you on this code which is not "clean" (you answered me the same thing on this other post where I asked how to add an additional translation line), but we can do anything to do with Node Red even word processing right ? 😜  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 May 2023 10:41 UTC](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/18 "2023-05-10T10:41:41Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
