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

Hi,

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

/*
* 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!

[{"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

That seems to be C code?

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.

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

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

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

I tried file.split('/') and here it is:
image

Oh that code is your data. I misunderstood.

You could do something like this

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.

1 Like

Try

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

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

will only replace first occurance.
it should be

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

for multiple replaces.

2 Likes

Thanks, @jbudd looks like it doesn't resolve all the \n :
image

@E1cid's answer is much better than mine.

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

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

image
the original text after the let lines = file.split(/(?<!:)\n/)

image
the new File modified and saved with a "write file node"

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

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


the result is that I only have one line. I can't process my text behind line by line.

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

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

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.

wow that's awesome that's all I ask you're amazing :star_struck:

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 ? :stuck_out_tongue_winking_eye:
Thanks a lot

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