基于Javascript中的换行符分析子字符串中的textarea

基于Javascript中的换行符分析子字符串中的textarea,javascript,Javascript,我需要解析一个文本区域。每一条新的生产线都需要拔掉,并且需要对其进行操作。操作完成后,需要在下一行运行该操作。这就是我现在拥有的。我知道索引搜索不起作用,因为它是一个字符一个字符地搜索 function convertLines() { trueinput = document.getElementById(8).value; //get users input length = trueinput.length; //getting the length of the user input

我需要解析一个文本区域。每一条新的生产线都需要拔掉,并且需要对其进行操作。操作完成后,需要在下一行运行该操作。这就是我现在拥有的。我知道索引搜索不起作用,因为它是一个字符一个字符地搜索

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);
}
函数转换行()
{
trueinput=document.getElementById(8).value;//获取用户输入
length=trueinput.length;//获取用户输入的长度
newinput=trueinput;//我知道这看起来很傻,但我以后会用到所有这些
userinput=newinput;
multiplelines=false;//这是一个检查,看看我以后是否应该使用if语句
for(var i=0;i”)=-1)//检查字符是否相同
{
//找到换行符,将其解析并对其运行操作
userinput=newinput.substring(0,i+1);
submitinput(用户输入);
newinput=newinput.substring(i+1);
多线=真;
}   
}
如果(多条线==假)
submitinput(用户输入);
}

因此,在很大程度上,它是接受用户输入的。如果它有多行,它将运行每行,并分别运行submitinput。如果你们能帮助我,我将永远感激你们。如果您有任何问题,请询问

如果用户正在使用enter键转到您可以写入的文本区域的下一行

var textAreaString = textarea.value;
textAreaString = textAreaString.replace(/\n\r/g,"<br />");
textAreaString = textAreaString.replace(/\n/g,"<br />");

textarea.value = textAreaString;
var textAreaString=textarea.value;
textAreaString=textAreaString.replace(/\n\r/g,“
”); textAreaString=textAreaString.replace(/\n/g,“
”); textarea.value=textAreaString;
文本区域的
内的换行符由换行字符(在大多数浏览器中是
\r\n
,在IE和Opera中是
\n
)表示,而不是HTML

元素,因此可以通过将换行符规范化为
\n
然后调用
拆分()来获取单独的行
文本区域值的方法。下面是一个实用函数,它为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]";
});