javascript 根据Javascript中的换行符解析子字符串中的textarea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3800795/
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
parse a textarea in substrings based on line breaks in Javascript
提问by Reid Frahm
I have a text area that I need to parse. Each new line needs to be pulled out and an operation needs to be performed on it. After the operation is done the operation needs to be run on the next line. This is what I have at the moment. I know the indexOf search won't work because it's searching character by character.
我有一个需要解析的文本区域。需要拉出每条新线并对其进行操作。操作完成后,需要在下一行运行操作。这就是我目前所拥有的。我知道 indexOf 搜索不起作用,因为它正在逐个字符地搜索。
function convertLines()
{
trueinput = document.getElementById(8).value; //get users input
length = trueinput.length; //getting the length of the user input
newinput=trueinput; //I know this looks silly but I'm using all of this later
userinput=newinput;
multiplelines=false; //this is a check to see if I should use the if statement later
for (var i = 0; i < length; i++) //loop threw each char in user input
{
teste=newinput.charAt(i); //gets the char at position i
if (teste.indexOf("<br />") != -1) //checks if the char is the same
{
//line break is found parse it out and run operation on it
userinput = newinput.substring(0,i+1);
submitinput(userinput);
newinput=newinput.substring(i+1);
multiplelines=true;
}
}
if (multiplelines==false)
submitinput(userinput);
}
So for the most part it is taking the userinput. If it has multiply lines it will run threw each line and seperatly and run submitinput. If you guys can help me I'd be eternally thankful. If you have any questions please ask
所以在大多数情况下,它接受用户输入。如果它有多行,它将运行抛出每一行并单独运行提交输入。如果你们能帮助我,我将永远感激不尽。如果您有任何问题,请询问
回答by Tim Down
Line breaks within the valueof a textarea are represented by line break characters (\r\nin most browsers, \nin IE and Opera) rather than an HTML <br>element, so you can get the individual lines by normalizing the line breaks to \nand then calling the split()method on the textarea's value. Here is a utility function that calls a function for every line of a textarea value:
valuetextarea 中的换行符由换行符表示(\r\n在大多数浏览器中,\n在 IE 和 Opera 中)而不是 HTML<br>元素,因此您可以通过将换行符标准化为\n然后调用split()textarea 上的方法来获取各个行价值。这是一个实用函数,它为 textarea 值的每一行调用一个函数:
function actOnEachLine(textarea, func) {
var lines = textarea.value.replace(/\r\n/g, "\n").split("\n");
var newLines, i;
// Use the map() method of Array where available
if (typeof lines.map != "undefined") {
newLines = lines.map(func);
} else {
newLines = [];
i = lines.length;
while (i--) {
newLines[i] = func(lines[i]);
}
}
textarea.value = newLines.join("\r\n");
}
var textarea = document.getElementById("your_textarea");
actOnEachLine(textarea, function(line) {
return "[START]" + line + "[END]";
});
回答by Chinmayee G
If user is using enter key to go to next line in your text-area you can write,
如果用户使用回车键转到文本区域中的下一行,您可以编写,
var textAreaString = textarea.value;
textAreaString = textAreaString.replace(/\n\r/g,"<br />");
textAreaString = textAreaString.replace(/\n/g,"<br />");
textarea.value = textAreaString;

