Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse a textarea in substrings based on line breaks in Javascript

Tags:

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

like image 602
Reid Frahm Avatar asked Sep 27 '10 02:09

Reid Frahm


2 Answers

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]";
});
like image 121
Tim Down Avatar answered Oct 12 '22 11:10

Tim Down


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;
like image 20
Chinmayee G Avatar answered Oct 12 '22 13:10

Chinmayee G