Tech Sharing

Tech Sharing


Trim a string in Javascript

By on June 13, 2009

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, ”);



No Responses to “Trim a string in Javascript”

Write a Comment

Captcha *

%d bloggers like this: