Skip to content

Instantly share code, notes, and snippets.

@supercheetah
Created May 24, 2013 06:38
Show Gist options
  • Save supercheetah/5641688 to your computer and use it in GitHub Desktop.
Save supercheetah/5641688 to your computer and use it in GitHub Desktop.
A useful little tool for taking a bunch of lines, and changing the new line character to something else in just HTML/JavaScript.
<!DOCTYPE html>
<html>
<head>
<title>Break up lines</title>
<script>
function replace_lines()
{
var raw_input = document.getElementById("inputted_lines").value.split('\n');
var sep = document.getElementById("separator").value;
var max_length = parseInt(document.getElementById("max_chars").value);
var results = "<p>";
for (var current_line = 0, char_count = 0; current_line < raw_input.length;
current_line++)
{
if (raw_input[current_line].trim() == "")
continue;
if (max_length > 0)
if (char_count + raw_input[current_line].trim().length + 1 > max_length) {
results=results.slice(0,results.length-1);
results += "</p><p>";
char_count = 0;
}
char_count += raw_input[current_line].trim().length + 1;
results += raw_input[current_line].trim() + sep;
}
results=results.slice(0,results.length-1);
results += "</p>";
document.getElementById("results_area").innerHTML=results;
}
</script>
</head>
<body>
<h1>Enter the lines to be separated here:</h1>
<textarea id="inputted_lines" rows="10" cols="40"></textarea>
<h3>What should each line be separated by? (Default is a
comma)</h3>
<input type="text" id="separator" maxlength="1" size="1" value="," />
<h3>What's the maximum number of characters that should be in each
block? (Zero for no limit)</h3>
<input type="text" id="max_chars" value="340" />
<input type="button" value="Submit" onclick="replace_lines()"/>
<h2>Results:</h2>
<div id="results_area"></div>
<p><em>Created by Rene Horn, 2013.</em></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment