Newline \n problem in JS
Solution 1
I found my own solution to my problem.
After using random special characters, \r character used for carriage return worked like a charm for my problem.
It acted like a newline \n character or at least it did its job in my case.
Thanks everyone for answers and helpful comments.
Solution 2
Try using this line
/\n/
Instead of this one
"\n"
Oralet
Updated on June 04, 2022Comments
-
Oralet almost 2 years
I am reading a file with
xmlHttp
object and splitting theresponseText
with newlines by split method.But "\n" character literally doesn't work. It acts like an error in my code and causes my code not even function. Here is the line:
var lines=myPlaylist.responseText.split("\n");
There is no error if I split the array myPlaylist with other characters. Just \n causes problem which I fail to understand.
At first, I thought the error was due to white-space:nowrap since I execute my code on Chrome. Though I never used white-space in anywhere, I tried to set it to normal but it didn't work. Similarly, I tried my code on other browsers (Firefox, IE etc), it didn't work either. Looks like I have a problem with using \n. Is there any other way to use newline or error with my code?
And by the way, error seems to be a syntax error since it does not just ignore \n character. Simply causes my code not to work
EDIT: An example
responseText
[playlist] File1=http://localhost:7000/Videos/Big%20Buck%20Bunny%20Trailer.ogv Title1=Bunny Trailer Length1=23 File2=http://localhost:7000/Videos/Dizzy%20Cat%20Video.ogv Title2=Kedi Length2=33 NumberOfEntries=2 Version=2
-
Olical over 13 yearsLooks like Chuck has the answer though.
/\r\n|\r|\n/
-
Oralet over 13 yearsmyPlaylist is defined like this : var myPlaylist = new XMLHttpRequest(); So it must have that property. And i couldn't execute mycode with your addition. Seems like problem still continues
-
Olical over 13 yearsOh! None of us suggested to use the global flag! I think that may be what wrong. @cantbereached Try some of the things you have tried before with a g after the RegExp.
-
Olical over 13 years@cantbereached Regarding the AJAX, you may want to allow for cross browser like this because your method won't work in IE: github.com/Wolfy87/Spark/blob/master/src/ajax.js#L3
-
Martijn over 13 years@cantbereached: what browser are you using? Does the Javascript console have anything useful to say?
-
Oralet over 13 yearsI use both chrome and firefox. My program is kind of a file browser and it functions with keyboard navigation. When I use \n or any of these solution, I can't even navigate through menus. That's why I think it is a syntax problem
-
Martijn over 13 yearsThree questions: 1) does the Javascript console in Firefox say anything? 2) What does: jsbin.com/amano4 do? 3) Are you using any server-side code in your page (e.g. PHP)?
-
Oralet over 13 years1) No 2) An alert box saying "Lines found: 3" and Hello World on the page 3) No
-
Martijn over 13 yearsOk, so there’s something in your page that’s breaking JS or the JS parser, since that jsbin page uses the exact code from my answer above. Does it work if you extract the Javascript, save it to a separate file, and reference that using
<script src="file.js"></script>
? -
Oralet over 13 yearsI tried your suggestion but no good. I can't use anything meaning newline anywhere in my code. It creates error. I tried to add \n to an innerText and it didn't work either :/
-
Martijn over 13 yearsReally weird. Your page isn't publicly available, by any chance?
-
Martijn over 13 yearsAnother suggestion: since
\n
seems to break your code, does it work when you avoid the\n
notation, e.g. withsplit(String.fromCharCode(10))
? -
Oralet over 13 yearsI found it mate. I just used \r for newline instead of using \n or \r\n or /\n/. Thanks for your sincere help
-
WestCoastProjects over 8 yearsi accidentally hit downvote. Just upvoted to "make up" for it ;)
-
J.Ko over 5 yearsVoting up because this worked for me. In my case, the string I had to split was from
JSON.stringify(str)
so new line chars\n
was actually literally\n
So this helped