+ 12

In Javascript can I split a string according to both spaced and newlines?

Is it possible? I mean I know how to split a text according to spaces but can I do that with newlines too? and is it possible to use both for the same text?(like for example if I have: begin var variable : integer ; end. I'd like to have it like this : begin,var,variable,:,integer,;,end.)

17th Aug 2017, 4:49 PM
C.E.
C.E. - avatar
12 ответов
+ 11
https://code.sololearn.com/Wq53YUNTg1W4/?ref=app Okay, so I'm not sure how you are getting new lines in a string... but this solution works. Don't need to use the same method I did for turning the array created by split into string, but it needs to be done to get res to split a second time. Anyway, hope this helps, seems to do what you want
17th Aug 2017, 5:37 PM
Russel Reeder
Russel Reeder - avatar
+ 16
// Yeah, this works well...(regex) var str = "begin\n var variable : integer ;\nend."; console.log(str.replace(/\n/g, " ").split(" "));
17th Aug 2017, 5:21 PM
Dev
Dev - avatar
+ 9
res = str.split(" "); res2 = res.split("\n"); document.write(res2); -- I'm gonna check this on codeplayground. Might be more complicated than I'm making it. EDIT: \\n here, and yes, just a little more complicated
17th Aug 2017, 5:14 PM
Russel Reeder
Russel Reeder - avatar
+ 8
If u need more help later share the code.
17th Aug 2017, 7:38 PM
Russel Reeder
Russel Reeder - avatar
+ 8
Thank you, I am making the code for this app so I'll post it anyways😉
17th Aug 2017, 8:07 PM
C.E.
C.E. - avatar
+ 7
Russel, I have new lines because I'm working with text areas and I want ti split the textarea.value acording to that. I'll try understeand your code 😉 Thanks all of you for helping me 😁
17th Aug 2017, 7:15 PM
C.E.
C.E. - avatar
+ 5
Here... I updated this cause I was bored. I am using Dayve's replace and split method, and put the string in a text area. Works 👍 https://code.sololearn.com/Wq53YUNTg1W4/?ref=app
18th Aug 2017, 6:41 PM
Russel Reeder
Russel Reeder - avatar
+ 4
yourstr.split(/\n|\s/gm); or if you want \n\n\n as single \n and \s\s\s as single \s yourstr.split(/(\n|\s)+/gm) I don't understand why that 2 guys use too long code
18th Aug 2017, 10:47 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
I m not sure about it whether it will work or not but the best of knowing it is to trying it... And after I m done with my ongoing code I will try going through it...
18th Aug 2017, 12:44 PM
Aditya Sharma
Aditya Sharma - avatar
+ 2
Ok that still keeping on using .replace ....
19th Aug 2017, 7:12 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 1
Hello @Elizabeth Espindola ! Did you know the textarea "cols" and "rows" attribute ? By default a line in a textarea is 20char, you can use those attr to define the size instead of CSS. This way you can make a function that deals with new line every 20char. If you want me to write that function or if you want more help please contact me. https://www.w3schools.com/tags/att_textarea_cols.asp
19th Aug 2017, 10:12 AM
coutable.n
coutable.n - avatar
- 2
absolutly u can
19th Aug 2017, 2:17 PM
Sarifudin Z
Sarifudin Z - avatar