---
title: "JavaScript String trim"
author: "ieniac"
date: "2011-12-17T10:24:01Z"
tags: []
categories: ["Web前端"]
slug: "javascript-string-trim"
draft: false
lastmod: "2011-12-17T10:24:01Z"
id: 1314476
url: "https://ieniac.iteye.com/blog/1314476"
---

```
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}
```