javascript 如何在html文本区域中逐行读取文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17595361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read text line by line in a html text area?
提问by mickeymoon
How can I read the contents of a text area line by line or split the text in different lines to obtain input as lines of text.
如何逐行读取文本区域的内容或将文本拆分为不同的行以获取作为文本行的输入。
回答by SLaks
You're looking for the Javascript split()
method.
您正在寻找 Javascriptsplit()
方法。
回答by Ganesh Rengarajan
Try this one....
试试这个....
you can split by "\n" and then get lilne by line value using for loop
您可以按 "\n" 拆分,然后使用 for 循环按行值获取 lilne
var lines = $('textarea').val().split('\n');
for(var i = 0;i < lines.length;i++){
//code here using lines[i] which will give you each line
}
回答by Suresh Atta
You can achieve it by using split()
method.
您可以通过使用split()
方法来实现它。
var splittedLines= inputString.split('\n');
And where splittedLines
is an array
.Loop through splittedLines
to get the each line.
哪里splittedLines
是array
.Loop 通过splittedLines
获取每一行。
回答by anmarti
var arr = yourtext.split("\n");
var arr = yourtext.split("\n");
loop arr to get each line.
循环 arr 获取每一行。
回答by Riv
you can convert the value of the textarea to an array by splitting on the newline \n character
您可以通过在换行符上拆分来将 textarea 的值转换为数组 \n 字符