Trim a string in Javascript


Trim is a good features when you try to delete the space at the left most, right most or both the left most and right most of the string. The following trim function is ultilize the javascript Regular expression for the trim function


Left Trim

function ltrim(str){

    return str.replace(/^s+/, ”);

}


Right Trim

function rtrim(str) {

    return str.replace(/s+$/, ”);

}


All trim

function alltrim(str) {

    return str.replace(/^s+|s+$/g, ”);

}


Leave a Reply

google.com, pub-3772983857049267, DIRECT, f08c47fec0942fa0
%d bloggers like this: