trim, ltrim and rtrim in javascript

Trim , Left trim (ltrim) and Right Trim (rtrim) in javascript


//Function to trim the space in the left side of the string
function ltrim ( s ){
return s.replace( /^s*/, "" );
}

//Function to trim the space in the right side of the string
function rtrim ( s ){
return s.replace( /s*$/, "" );
}

//*Function to trim the space in the  string
function trim(s) {
var temp = s;
return temp.replace(/^s+/,'').replace(/s+$/,'');

ltrim() will remove all the spaces to the left in a string rtrim() will remove all the spaces to the right in a string trim() will all the left , right and spaces inside the string Ex: var x = ” thestring”; ltrim(x) will return “thestring”; var y = “thestring “; rtrim(y) will return “thestring”; var z = ” the st ri ng “; trim(z) will return “thestring”

3 Replies to “trim, ltrim and rtrim in javascript”

  1. You missed out the ‘\’ char
    return s.replace( /^s*/, ?? );
    Should be
    return s.replace( /^\s*/, ?? );
    in all cases.

    I think it is a bug in WordPress. See PHP’s stripslash function.

  2. Pingback: cuhulehiyucn
  3. how to remove left side of this string value zero after removing first four digit and delete all zero until we get the 102 number by using javascript. The digit
    000j0000000000000102

Leave a Reply to Binny V A Cancel reply

Your email address will not be published. Required fields are marked *