Regular Expression based Javascript Trim

There are several ways to trim a string in Javascript. Here are some examples of Trim Function by Regular Expression.

Trim Left

function trimLeft(str)
{
return str.replace(/^\s+/g, "");
}

Trim Right

function trimRight(str)
{
return str.replace(/\s+$/g, "");
}

Trim Both

function jsTrim(str)
{
return str.replace(/^\s+|\s+$/g, "");
}

Trim

This entry was posted in Javascript. Bookmark the permalink.