How To Add Trim Functionality To Javascript Stringトリムの機能を追加する方法をJavaScriptの文字列
Trim is a useful function available in languages like Java & PHP which removes the leading and traling whitespace(s) from a String.トリムは、便利な機能がご利用の言語でのJava & PHPのような大手とtraling空白文字を削除する(秒)より、文字列です。 Unfortunately Javascript doesn’t natively provide trim functionality to the String object.残念ながらJavaScriptをネイティブではありませんトリムの機能を提供する文字列オブジェクトを作成します。 Fortunately there is a simple solution.幸いには、シンプルなソリューションです。 Place the following code near the top of your Javascript file (or inline script) to add trim() functionality to all String objects.場所は、次のコードの上部にしてJavaScriptファイル(またはインラインスクリプト)を追加するトリム( )の機能をすべての文字列オブジェクトです。
String prototype.trim = function() { a = this.replace(/^\s+/, ''); return a.replace(/\s+$/, ''); }; 文字列prototype.trim =関数( ) (する= this.replace ( / ^ \秒+ / 、 '' ) ;戻りa.replace ( / \秒+ $ / 、 '' ) ; ) ; This underlines one of the beauty (or beast depending on who you ask) of Javascript - the ability to add functionality to an existing class, even native classes like String, by accessing its prototype.この下線の1つの美しさ(またはwhoことを聞くのに応じてビースト)のJavaScriptを-する能力を既存のクラスに機能を追加するも、ネイティブクラスのような文字列を、そのプロトタイプにアクセスします。
Now you can use trim() on any String in your code.今すぐ使用することができますトリム( )を任意の文字列のコードです。
Filed under提出されて Javascript JavaScriptの , 、 Programmingプログラミング , 、 Webウェブ , 、 Web 2.0ウェブ2.0 | |
| |
RSS 2.0 RSS 2.0を | |
Trackbackトラックバック this Article |この記事|
Email this Article電子メールこの記事
You may also like to readを読むようにすることも可能 |





September 16th, 2007 at 11:50 pm 2007年9月16日11:50 pmの
Hi Sir,こんにちは卿、
The topic is very useful.But in this form it will not work.We have to put dot(.) after String instead of space.のトピックは非常にuseful.butこのフォームを置くことはありませんがwork.weドット( 。 )スペースの代わりにした後の文字列です。
String.prototype.trim = function() { string.prototype.trim =関数( ) (
a = this.replace(/^s+/, ”);を= this.replace ( / ^秒+ / 、 " ) ;
return a.replace(/s+$/, ”);戻りa.replace ( /秒+ $ / 、 " ) ;
}; ) ;
December 27th, 2007 at 11:18 pm 2007年12月27日は11:18 pmの
Sir,卿、
I am striking with trim() option in Java script私は目を見張るにトリム( )オプションでJavaスクリプト
I have a one string,私には1つの文字列、
A = “E:\ram\Collections\t1.jpeg”を= "メール: \ラム\コレクション\ t1.jpeg "
I need,私が必要、
ans = “E:\ram\Collections\” only答え= "メール: \ラム\コレクション\ "のみ
Please kindly help me.ご親切に協力をお願いします。 I can use the trim function but it not use.私はそれを使用してトリム機能を使用しない。
Thanks & Regardsありがとう&について
TRHarihara sudhan trharihara sudhan