Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

parse a textarea in substrings based on line breaks in Javascript

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

var textarea = document.getElementById("your_textarea");
actOnEachLine(textarea, function(line) {
    return "[START]" + line + "[END]";
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...