Text Processing Tools
Every modern web browser has numerous handy text processing functions, built right into the JavaScript programming language. Splitting text, getting the length, or changing the letter case can often be done in a single line of code! Unfortunately, writing code is an inconvenient barrier, especially for those who aren't familiar with it. This free webpage provides access to many of these features through simple buttons that apply snippets of code to text.
Put your text in the box below, and click a button to apply that function to the text. Click the > button to expand and edit the code. The input string is provided in the "text" variable. The function can either return a string value, or write to the "output" string variable.
These additional JavaScript functions are defined to make common operations easier. You can call them in any of the button code snippets:
const PUNCTUATION = ".!?";
const WHITESPACE = " \t\r\n";
const LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Advances the index i past any characters in s that exist in chars, and
// returns the index of the first character that doesn't match, or the length of s.
function skipChars(s, chars, i) {
while (i < s.length && chars.indexOf(s[i]) != -1) i++;
return i;
}
// Advances the index i past any characters in s that don't exist in chars, and
// returns the index of the first character that does match, or the length of s.
function skipNotChars(s, chars, i) {
while (i < s.length && chars.indexOf(s[i]) == -1) i++;
return i;
}
// Splits a string into an array of sentences, where each string starts with the
// first letter of the first word of the sentence. Any whitespace is left on the
// end of the previous sentence so the string can be losslessly reconstructed.
String.prototype.splitSentences = function() {
var li = skipChars(this, WHITESPACE, 0);
var i = li;
var w = [];
while (i < this.length) {
i = skipNotChars(this, PUNCTUATION, i);
i = skipChars(this, PUNCTUATION, i);
i = skipChars(this, WHITESPACE, i);
w.push(this.substring(li, i));
li = i;
}
return w;
}
// Splits a string into an array of words, and removes all whitespace and
// special characters. Does not alter the character case.
String.prototype.extractWords = function() {
var li = skipNotChars(this, LETTERS, 0);
var w = [];
while (li < this.length) {
var i = skipChars(this, LETTERS, li);
w.push(this.substring(li, i));
li = skipNotChars(this, LETTERS, i);
}
return w;
}
// Splits a string into an array of words, where each string starts with the
// first letter of the word. Any whitespace is left on the end of the previous
// word so the string can be losslessly reconstructed.
String.prototype.splitWords = function() {
var li = skipNotChars(this, LETTERS, 0);
var w = [];
while (li < this.length) {
var i = skipChars(this, LETTERS, li);
i = skipNotChars(this, LETTERS, i);
w.push(this.substring(li, i));
li = i;
}
return w;
}
// Applies a function to every element in an array.
Array.prototype.each = function(parser) {
for (var c = 0; c < this.length; c++) {
this[c] = parser(this[c]);
}
return this;
}
// Applies String.toLowerCase() to every string in an array.
Array.prototype.toLowerCase = function() {
for (var c = 0; c < this.length; c++) {
this[c] = this[c].toLowerCase();
}
return this;
}
// Applies String.toUpperCase() to every string in an array.
Array.prototype.toUpperCase = function() {
for (var c = 0; c < this.length; c++) {
this[c] = this[c].toUpperCase();
}
return this;
}
The code for this webpage is open source under the MIT license.
