0

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?

Edited by benqus: n/a

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by benqus
0

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?

0

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.

0

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!");
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.