Javascript Course

The cleanLongWords() function presented in this page can be used to clean text with multiple consecutive characters and long words in JavaScript. Useful to clean texts that users add in input text fields or textarea.
By default, the function allows maximum 2 consecutive repetitions of characters in words, but not affects the numbers, and split the word to maximum 23 characters. See the comments in code, and example below.

Code of the cleanLongWords() function

- Click to select it.
function cleanLongWords(str, nrchr, maxword) {
// function to clean text with multiple consecutive characters and very long words ( https://coursesweb.net/ )
// nrchr = allowed number of consecutive character
// maxword = maximum word length
  if(!nrchr) var nrchr = 2;
  if(!maxword) var maxword = 23;
  var chr = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l',';','z','x','c','v','b','n','m',',','!','@','#','%','&','_','=',':','"','`','~',';','â','á','é','í','ó','ú','ý','ø','č','ć','đ','š','ž','ā','ä','ǟ','ḑ','ē','ī','ļ','ņ','ō','ȯ','ȱ','õ','ȭ','ŗ','š','ț','ū','ž','ş','î','ă',"'",'$','\\^','\\*','\\(','\\)','\\{','\\}','\\|','\\?','\\.','\\[','\\]','\\/','\\\\','\\>','\\<'];

  // uncomment next line if you want to clean consecutive numbers too
///  chr.push('1','2','3','4','5','6','7','8','9','0');
  var n_chr = chr.length;

  function wordwrap(str, maxword, str_break, cut) {
    // Splits a string to a given number of characters: http://phpjs.org/functions/wordwrap/
    var m = ((arguments.length >= 2) ? arguments[1] : 75);
    var b = ((arguments.length >= 3) ? arguments[2] : '\n');
    var c = ((arguments.length >= 4) ? arguments[3] : false);
    var i, j, l, s, r;
    str += '';

    if (m < 1) return str;

    for (i = -1, l = (r = str.split(/\r\n|\n|\r/)).length; ++i < l; r[i] += s) {
      for (s = r[i], r[i] = ''; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : '')) {
        j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/))[0].length;
      }
    }

    return r.join('\n');
  }

  for(var i=0; i<n_chr; i++) {
    var patern = new RegExp('(['+ chr[i] +']{'+ nrchr +',}){2,}', 'ig');
    str = str.replace(patern, '$1');
  }

  // if maxword > 0, split the word to specified number of characters
  return (maxword > 0) ? wordwrap(str, maxword, ' ', true) : str;
}

Example usage

<script>
// Here Add the cleanLongWords() function

var text = '\\\\\\\\\  A ttttttteeeeeeeeexxxxxxt with loooooonnnnnngggggg woooooooooooords and coooooonnnnnnsssssseeeeeeccccccuuuuuuttttttiiiiiivvvvvveeeeeee characteeeeeerrrrrrssssss [[[[[]]]]]] ///////......';

var text1 = cleanLongWords(text);
var text2 = cleanLongWords(text, 3);    // allow 3 consecutive repetitions of characters
var text3 = cleanLongWords(text, 1, 0);    // not consecutive characters, and not split the word

alert('text1 - '+ text1);
alert('text2 - '+ text2);
alert('text3 - '+ text3);
</script>
- Result:
text1 - \\ A tteexxt with loonngg woords and coonnsseeccuuttiivvee characteerrss [[]] //..
text2 - \\\\ A ttteeexxxt with looonnnggg wooords and cooonnnssseeecccuuuttti iivvveee characteeerrrsss [[[[[]]] ///...
text3 - \ A text with long words and consecutive characters [] /.

Live test



Result:
Here add some text. Will be cleaned keeping 2 consecutive characters and word to maximum 23 characters

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Delete multiple consecutive characters and Split long words in JavaScript

Last accessed pages

  1. PHP PDO - prepare and execute (9187)
  2. jQuery Ajax - load() method (10835)
  3. Creating XML data - E4X in ActionScript 3 (3088)
  4. Introduction to ActionScript 3 (3380)
  5. Accesing XML data - E4X (1436)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (468)
  2. CSS cursor property - Custom Cursors (78)
  3. The Mastery of Love (69)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)