ホームページ  >  記事  >  ウェブフロントエンド  >  JavaScriptのカスタム共通メソッド

JavaScriptのカスタム共通メソッド

PHP中文网
PHP中文网オリジナル
2016-05-16 18:47:541005ブラウズ

例: ページの文字処理、js 正規表現の検証など。次に、私自身の浅い開発経験とインターネット上の膨大なリソースを要約して整理し、将来それを使用する必要があるときに検索する手間を省きます。この連載では、よく使われる機能をまとめて整理し、出発点として扱います。
コードは簡単です。コードを見てください:
1. 一般的な文字列処理関数

// 文字の長さを返します。1 つの中国語は 2 としてカウントされます。 = function( ) {
return this .replace( / [ ^ x00 - xff] / g, " ** " ).length;
}

// 両端の空白文字を削除文字列の
String.prototype.Trim = function() {
return this .replace( / ( ^ s ) | (s $) / g, "" ); >// 文字の左端を削除
String.prototype.LeftTrim = function() {
return this .replace( / ( ^ [s] * ) / g, "" ); の空白文字を削除します。
}

// 文字の右端の空白文字を削除
String.prototype.RightTrim = function() {
return this .replace( / ([s] * $) / g, "" );
}

/* 文字列が等しいかどうかを確認するために大文字と小文字の比較を無視します
注: 大文字と小文字を無視せずに比較するには、== を使用します*/
文字列.prototype.IgnoreCaseEquals = function(str) {
return this.toLowerCase() == str.toLowerCase();
}

/* 文字列が等しいかどうかを確認するために大文字と小文字の比較を無視しません。 */
String.prototype.Equals = function(str) {
return ( this == str);
}

/* 文字列を比較し、条件に従って -1、0 を返します。結果
同じ戻り値: 0 異なる: -1
*/
String.prototype.CompareTo = function(str) {
if ( this == str) {
return 0 ; 🎜>} else
return - 1 ;
}

// 文字列置換
String.prototype.Replace = function(oldValue, newValue) {
var reg = new RegExp( oldValue, " g " );
return this .replace(reg, newValue);

// 特定の文字列で終わるかどうかを確認します
String.prototype.EndsWith = function (str) {
return this .substr( this .length - str .length) == str;
}

// 文字列が指定された文字列
String で始まるかどうかを判断します。 prototype.StartsWith = function(str) {
return this .substr (0, str.length) == str;
}

//
String から n 文字を切り取ります。 prototype.LeftSlice = function(n) {
return this .substr (0, n);
}

// 右から n 文字を切り取ります
String.prototype.RightSlice = function (n) {
return this .substring( this .length - n);
}

// 指定された文字の出現数をカウントします
String.prototype.Occurs = function (ch) {
// var re = eval("/[^" ch "]/g");
// return this.replace(re, "").length
return this; .split(ch).length - 1 ;
}


/* */
String.prototype.Style = function(style) { を使用して、特定のスタイルで文字列を構築します。
return "
" , this , "
" );
}

// StringBuilder と同様の関数を構築します (複数の文字列を結合するときに使用され、非常に便利です)
function StringBuilder(str) { this .tempArr = new Array() ; StringBuilder.prototype.Append = function(value) {
this .tempArr.push(value); >これを返します ;
}
StringBuilder.prototype.Clear = function() {
this .tempArr.length = 0 ;
}
StringBuilder.prototype.toString = function() 🎜>return this .tempArr.join( '' );
}

//共通の文字列メソッドと拡張機能
function test() {
var testStr = " これはテスト文字列です" ;
var testStr2 = " String" ;
// アラート(testStr.Trim());
// アラート(testStr.LeftTrim());
// アラート(testStr.RightTrim ());
// アラート(testStr2. ChineseLength( ));
// アラート(testStr2.CompareTo(testStr));
// document.write(testStr2.Style(" color:red;width:100px"));
//alert(testStr2.LeftSlice(2)); (7));
//alert(testStr.Occurs("s"));
/* StringBuilder テスト*/
// var testStr3 = new StringBuilder(""); / testStr3.Append("test3rn");
/ / testStr3.Append("test3test3rn");
// testStr3.Append("test3"); );
// testStr3.Clear();
//alert(testStr3.toString());

2. * ------------- -----------次の関数にはまだ文字列処理が含まれていますが、正規表現の一部として使用する方が合理的だと思われます-- ------------- ------------- */
// 文字列が数字で構成されているかどうかを確認します
String.prototype.IsDigit = function () {
var str = this .Trim ();
return (str.replace( / d / g, "" ).length == 0 ); / 文字列が浮動小数点型であるかどうかを検証します
String.prototype.IsFloat = function () {
var str = this .Trim()
// 空の場合は検証に合格しません。
if (str == "" )
return false ;
// If it is an integer, check the validity of the integer
if (str.indexOf( " . " ) == - 1 ) {
return str.IsDigit();
}
else {
if ( / ^(-?)(d )(.{1})(d )$ / g.test(str))
return true ;
else
return false ;
}
}

// Check whether it is a negative integer
function isNegativeInteger(str) {
// If it is empty, the verification will not pass
if (str == "" )
return false ;
if (str.IsDigit()) {
if (parseInt(str, 10 ) < 0 )
return true ;
else
return false ;
}
else
return false ;
}

// Check whether it is a negative floating point number
function isNegativeFloat(str) {
// If it is empty, the verification will not pass
if (str == "" )
return false ;
if (str.IsFloat()) {
if (parseFloat (str, 10 ) < 0 )
return true ;
else
return false ;
}
else
return false ;
}

/ / Is it a string composed of letters?
function isCharacter(str) {
return ( / ^[A-Za-z] $ / .test(str));
}

// Whether it is a string of letters and numbers
function isNumberCharacter(str) {
return ( / ^[A-Za-z0-9] $ / .test(str));
}

// Whether it is email
function isEmail(str) {
return ( / (S) [@]{1}(S) [.]{1}(w) / .test (str))
}

// Is it a url (Comment: The version circulating on the Internet has very limited functions, the following one can basically meet the needs)
function isUrl(str) {
return ( / ([a-zA-z] : / / )?[^s]* / .test(str));
}

// Is it an ip address?
function isIpAddress( str) {
return / (d ).(d ).(d ).(d ) / .test(str);
}

// Is it a string composed of Chinese characters
function isChinese(str) {
return ( / ^[u4e00-u9fa5] $ / .test(str));
}

// Whether it is a double-byte character (including Chinese characters Within)
function isUnicode(str) {
return ( / ^[x00-xff] $ / .test(str));
}

// Is it a phone number
function isTelephone(str) {
// Compatible format: Country code (2 to 3 digits)-area code (2 to 3 digits)(-)?Phone number (7 to 8 digits)-extension number (3 digits) )
return ( / ^(([0 ]d{2,3}-)?(0d{2,3}))?[-]?(d{7,8})(-(d{3 ,}))?$ / .test(str));
}

// Is it a mobile phone number?
function isMobilePhone(str) {
return ( / ^(((d {3}))|(d{3}-))?1[3,5]d{9}$ / .test(str));
}

// Is it a QQ number? (Tencent QQ number starts from 10000)
function isQQNumber(str) {
return ( / ^[1-9][0-9]{4,}$ / .test(str));
}

// Is it a domestic postal code (China postal code is 6 digits)
function isMailCode(str) {
return ( / d{6} / .test(str)) ;
}

// Is it a domestic ID number?
function isIdNumber(str) {
return ( / d{15}|d{18} / .test(str) ; It’s just a piece of cake, so I won’t go into details.



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。