Hello again!

I need your help again, people! =)

So here's my problem:
This line replaces only the first "\n" spec. character in my textarea, and this way everything's screwed up... I'm sending the replaced content of the textarea through AJAX to the server, in a GET method.
I've tried to just comment out this line to see whether this is necesarry, but then no "\n" is transferred at all... I'm using a PHP server side, and that works.

Code:

var content=document.getElementById("editor").value;
content=content.replace("\n", "%nline!");

Or maybe should I split up the value of the textarea by "\n", and GET it to the server, and then write it out line by line?

Recommended Answers

All 4 Replies

Without the "global" flag, replace stops at the first instance.

Without the "global" flag, replace stops at the first instance.

Do you mean, that I should define the content variable outside of this function? Like on the beginning of the js file, and not in the AJAX method?

Do you mean, that I should define the content variable outside of this function? Like on the beginning of the js file, and not in the AJAX method?

First, you have used a string ("\n") instead of a regexp (/\n/) in the string.replace function. Second, string.replace will replace only the first occurrence of the pattern in the string unless the global flag is used. If you use a proper regexp and include the global flag, (/\n/g), you should find all NLs replaced as you desire.

Unless you are using a paleolithic JavaScript manual, this should all be clearly spelled out. Flanagan's 5th Edition (2006) from O'Reilly is about as old a version as you'd want to use.

My only problem is that there aren't so many hungarian translations... Because I'm not a pro in english, I prefer learning it it on my mother language. It'd be really nice if I'd find a good translation of a RegExp book, or something... Anything... That's why I'm asking stuff here...

Anyway, Thank you! Problem solved

var content=document.getElementById("editor").value
content=content.replace(/\n/g, "%nline!");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.