+ 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.)
12 Respostas
+ 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
+ 16
// Yeah, this works well...(regex)
var str = "begin\n var variable : integer ;\nend.";
console.log(str.replace(/\n/g, " ").split(" "));
+ 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
+ 8
If u need more help later share the code.
+ 8
Thank you, I am making the code for this app so I'll post it anywaysđ
+ 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 đ
+ 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
+ 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
+ 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...
+ 2
Ok that still keeping on using .replace ....
+ 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
- 2
absolutly u can