javascript trim方法
在javascript中的string对象没有trim方法,所以trim功能需要自己实现:
代码如下:
Java代码
﹤scriptlanguage=”javascript”﹥
/**
*删除左右两端的空格
*/
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g, '');
}
/**
*删除左边的空格
*/
String.prototype.ltrim=function()
{
return this.replace(/(^s*)/g,'');
}
/**
*删除右边的空格
*/
String.prototype.rtrim=function()
{
return this.replace(/(s*$)/g,'');
}
﹤/script﹥
﹤scriptlanguage=”javascript”﹥
/**
*删除左右两端的空格
*/
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g, '');
}
/**
*删除左边的空格
*/
String.prototype.ltrim=function()
{
return this.replace(/(^s*)/g,'');
}
/**
*删除右边的空格
*/
String.prototype.rtrim=function()
{
return this.replace(/(s*$)/g,'');
}
﹤/script﹥
使用如下
Java代码
﹤scripttype=”text/javascript”﹥
alert(document.getElementById(’abc’).value.trim());
alert(document.getElementById(’abc’).value.ltrim());
alert(document.getElementById(’abc’).value.rtrim());
﹤/script﹥
相关文档:
第七章 Ajax 异步JavaScript和XML
Ajax is a cornerstone of high-performance JavaScript. It can be used to make a page load faster by delaying the download of large resources. It can prevent page loads altogether by allowing for data to be transferred between the client ......
Data Formats 数据格式
When considering data transmission techniques, you must take into account several factors: feature set, compatibility, performance, and direction (to or from the server). When considering data formats, the only scale you need for comparison is speed.
......
第八章 Programming Practices 编程实践
Every programming language has pain points and inefficient patterns that develop over time. The appearance of these traits occurs as people migrate to the language and start pushing its boundaries. Since 2005, when the term "Ajax" ......
Working Around Caching Issues 关于缓存问题
Adequate cache control can really enhance the user experience, but it has a downside: when revving up your application, you want to make sure your users get the latest version of the static content. This is accomplished by renaming ......
javascript的String类内置函数replace(regexp, newString)函数提供了字符串替换功能,从函数原型上可以看出支持 Regular Exp。此函数功能非常实用,但也有几个地方有点混淆,下面通过实际的例子来说明:
比如我们现在要针对串
var src="<a href="http://xx.com/a/2010 ......