根据Javascript中的换行符解析子串中的textarea [英] parse a textarea in substrings based on line breaks in Javascript

查看:92
本文介绍了根据Javascript中的换行符解析子串中的textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要解析的文本区域。需要拔出每个新线路并且需要对其执行操作。操作完成后,需要在下一行运行操作。这就是我现在所拥有的。我知道indexOf搜索不起作用,因为它逐字符搜索。

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.

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);
}

所以大部分都是用户输入。如果它有多个行,它将运行抛出每一行并单独运行submitinput。如果你们能帮助我,我会永远感恩。如果您有任何疑问,请询问

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

推荐答案

值内的换行符 textarea的字符串由换行符表示( \\\\ n 在大多数浏览器中, \ n in IE和Opera)而不是HTML < br> 元素,因此您可以通过将换行符规范化为 \ n来获取各行。 / code>然后在textarea的值上调用 split()方法。这是一个实用程序函数,它为textarea值的每一行调用一个函数:

Line breaks within the value of a textarea are represented by line break characters (\r\n in most browsers, \n in IE and Opera) rather than an HTML <br> element, so you can get the individual lines by normalizing the line breaks to \n and 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:

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]";
});

这篇关于根据Javascript中的换行符解析子串中的textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆