JavaScript trim string

In JavaScript, we don't have built-in functions to trim a string (removing spaces from the string) that support in all major browsers. However, It is not over our head. The Regular Expression can help us.


Remove spaces at the beginning of string (left trim)


To remove spaces at the beginning of the string, you need a way to match the beginning spaces. The way I want to talk here is string pattern. For example, to match the beginning spaces of a string, the string pattern can be written as below:
pat=/^ +/
You can add gi (global and case-insensitive) to the string pattern to make global search and case-insensitive.
pat=/^ +/gi
-^ character is to match the pattern at the beginning of the string.
-+ is to match the pattern one or more times. The spaces at the beginning of the string may be many. So, to match all the beginning spaces we write space character followed by + character (pressing spacebar one time and +).

After the string pattern is correctly defined, you need to call the replace(pat) method of the string object that contains the string value to be trimmed.

<html>
<head>
<title>JavaScript trim string</title>
<script type="text/javascript">

var s=ltrim("   JavaScript trim left spaces   ");
alert(s);

function ltrim(str){
var st=new String(str);
var pat=/^ +/gi; //matching beginning spaces in the string
st=st.replace(pat,""); //replace the beginning spaces with empty string
return(st); //return string after trimming
}
</script>

</head>
<body>
</body>
</html>

Remove spaces at the end of string (right trim)

To remove spaces at the end of the string, you need only to change the pattern from pat=/^ +/gi to pat=/ +$/gi.
The $ character is to match the pattern at the end of the string.
<html>
<head>
<title>JavaScript trim string</title>
<script type="text/javascript">

var s=rtrim("   JavaScript Code trim right spaces  ");
alert(s);

function rtrim(str){
var st=new String(str);
var pat=/ +$/gi; //matching trailing spaces in the string
st=st.replace(pat,""); //replace trailing spaces with empty string
return(st); //return string after trimming
}
</script>

</head>
<body>
</body>
</html>

Remove both beginning and trailing spaces (trim)

To remove all spaces at the beginning and at the end of the string, you need to group the above two patterns together. This involves using or operator in the pattern. So the pattern to match all spaces at the beginning and at the end of the string should be pat=/(^ +| +$)/gi.
<html>
<head>
<title>JavaScript trim string</title>
<script type="text/javascript">

var s=trim("   JavaScript Code trim both left and right spaces   ");
alert(s);

function trim(str){
var st=new String(str);
var pat=/(^ +| +$)/gi; //matching beginning and trailing spaces in the string
st=st.replace(pat,""); //replace the beginning and trailing spaces with empty string
return(st); //return string after trimming
}
</script>

</head>
<body>
</body>
</html>

Posted by: Dara | post date: 03-30-2013 | Subject: JavaScript




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.