The trim(), rtrim(), and ltrim() functions are generally known and used in PHP. These functions are useful when working with strings, but JavaScript doesn't have such functions.
Here's trim(), rtrim(), and ltrim() created manually in JavaScript:
// trim, rtrim, ltrim
function trim(str, chr) {
  var rgxtrim = (!chr) ? new RegExp('^\\s+|\\s+$', 'g') : new RegExp('^'+chr+'+|'+chr+'+$', 'g');
  return str.replace(rgxtrim, '');
}
function rtrim(str, chr) {
  var rgxtrim = (!chr) ? new RegExp('\\s+$') : new RegExp(chr+'+$');
  return str.replace(rgxtrim, '');
}
function ltrim(str, chr) {
  var rgxtrim = (!chr) ? new RegExp('^\\s+') : new RegExp('^'+chr+'+');
  return str.replace(rgxtrim, '');
}

      - trim() - Removes whitespace, or other character, from the beginning and end of a string.
      - rtrim() - Removes whitespace, or other character, from the end of a string.
      - ltrim() - Removes whitespace, or other character, from the beginning of a string.

How to use these trim, rtrim, and ltrim functions

To use these functions, first copy them in the code of your JS script.
If you want to remove whitespace, just pass a string as argument of the function; they will return that string with whitespace stripped from the beginning /end: trim(string).
To remove other character than whitespace, add that character as a second parameter (between quotes): trim(string, 'character').

Examples trim, rtrim, ltrim

1. Remove whitespace from the beginning /end of a string, then display the results in a HTML tag.
<pre id="sresult"></pre>
<script type="text/javascript"><!--
// JavaScript Course - https://coursesweb.net/javascript/
// Adding the trim(), rtrim(), ltrim() functions
function trim(str, chr) {
  var rgxtrim = (!chr) ? new RegExp('^\\s+|\\s+$', 'g') : new RegExp('^'+chr+'+|'+chr+'+$', 'g');
  return str.replace(rgxtrim, '');
}
function rtrim(str, chr) {
  var rgxtrim = (!chr) ? new RegExp('\\s+$') : new RegExp(chr+'+$');
  return str.replace(rgxtrim, '');
}
function ltrim(str, chr) {
  var rgxtrim = (!chr) ? new RegExp('^\\s+') : new RegExp('^'+chr+'+');
  return str.replace(rgxtrim, '');
}

var string1 = '   Initialy with 3 space characters to beginning and end   ';

// removing whitespace from beginning /end
var trim_re = 'trim(str): *'+ trim(string1)+ '*';
var rtrim_re = 'rtrim(str): *'+ rtrim(string1)+ '*';
var ltrim_re = 'ltrim(str): *'+ ltrim(string1)+ '*';

document.getElementById('sresult').innerHTML = trim_re+ '<br/>'+ rtrim_re+ '<br/>'+ ltrim_re;
--></script>

Results:
trim(str): *Initialy with 3 space characters to beginning and end*
rtrim(str): *     Initialy with 3 space characters to beginning and end*
ltrim(str): *Initialy with 3 space characters to beginning and end     *

2. Remove a specified character (/) from the beginning /end of a string.
<div id="sresult2"></div>
<script type="text/javascript"><!--
 /* Here add the trim(), rtrim(), ltrim() functions */

var string2 = '///Initialy with 3 "/" characters to beginning and end///';

// removing "/" from beginning /end
var trim_re = 'trim(str, chr): '+ trim(string2, '/');
var rtrim_re = 'rtrim(str, chr): '+ rtrim(string2, '/');
var ltrim_re = 'ltrim(str, chr): '+ ltrim(string2, '/');

document.getElementById('sresult2').innerHTML = trim_re+ '<br/>'+ rtrim_re+ '<br/>'+ ltrim_re;
--></script>

Results:
trim(str, chr): Initialy with 3 "/" characters to beginning and end
rtrim(str, chr): ///Initialy with 3 "/" characters to beginning and end
ltrim(str, chr): Initialy with 3 "/" characters to beginning and end///

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute indicates that an input field must be filled out before submitting?
required="required" autofocus="autofocus" placeholder="text"
<input type="text" name="name1" required="required" />
Which CSS method increases or decreases the size of the HTML element (including its content)?
translate() scale() skew()
#some_id:hover {
  transform: scale(2, 1.5);
  -ms-transform: scale(2, 1.5);    /* IE 9 */
  -moz-transform: scale(2, 1.5);   /* Firefox */
}
Click on the Date object method that returns the day of the month.
setDate() getMonth() getDate()
var rightnow = new Date();
var day = rightnow.getDate();
alert(day);
Which function is used to upload a file on server?
is_file() move_uploaded_file() fopen()
if(move_uploaded_file($_FILES["field_name"]["tmp_name"], "dir/file_name")) {
  echo "The file succesfully uploaded";
}
JavaScript trim, rtrim and ltrim

Last accessed pages

  1. Wake Up! (3403)
  2. Ajax-PHP Chat Script (37775)
  3. PHP-MySQL Scripts (3924)
  4. Create simple Website with PHP (20532)
  5. Select in MySQL, Output results in HTML Table (5636)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (415)
  2. Read Excel file data in PHP - PhpExcelReader (331)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (257)
  4. PHP getElementById and getElementsByTagName (208)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (199)