📄 regexp.js
字号:
// // flags:An object // flags.places The integer number of decimal places. // If not given, the decimal part is optional and the number of places is unlimited. // flags.decimal A string for the character used as the decimal point. Default is ".". // flags.fractional Whether decimal places are allowed. // Can be true, false, or [true, false]. Default is [true, false] // flags.exponent Express in exponential notation. Can be true, false, or [true, false]. // Default is [true, false], (i.e. will match if the exponential part is present are not). // flags.eSigned The leading plus-or-minus sign on the exponent. Can be true, false, // or [true, false]. Default is [true, false], (i.e. will match if it is signed or unsigned). // flags in regexp.integer can be applied. // assign default values to missing paramters flags = (typeof flags == "object") ? flags : {}; if(typeof flags.places != "number"){ flags.places = Infinity; } if(typeof flags.decimal != "string"){ flags.decimal = "."; } if(typeof flags.fractional == "undefined"){ flags.fractional = [true, false]; } if(typeof flags.exponent == "undefined"){ flags.exponent = [true, false]; } if(typeof flags.eSigned == "undefined"){ flags.eSigned = [true, false]; } // integer RE var integerRE = dojo.regexp.integer(flags); // decimal RE var decimalRE = dojo.regexp.buildGroupRE(flags.fractional, function(q){ var re = ""; if(q && (flags.places > 0)){ re = "\\" + flags.decimal; if(flags.places == Infinity){ re = "(" + re + "\\d+)?"; }else{ re = re + "\\d{" + flags.places + "}"; } } return re; } ); // exponent RE var exponentRE = dojo.regexp.buildGroupRE(flags.exponent, function(q){ if(q){ return "([eE]" + dojo.regexp.integer({ signed: flags.eSigned}) + ")"; } return ""; } ); // real number RE return integerRE + decimalRE + exponentRE; // String}dojo.regexp.currency = function(/*Object?*/flags){ // summary: Builds a regular expression to match a monetary value // // flags: An object // flags.symbol A currency symbol such as Yen "�", Pound "�", or the Euro sign "�". // Default is "$". For more than one symbol use an array, e.g. ["$", ""], makes $ optional. // flags.placement The symbol can come "before" the number or "after" the number. Default is "before". // flags.signPlacement The sign can come "before" the number or "after" the sign, // "around" to put parentheses around negative values, or "end" for the final char. Default is "before". // flags.cents deprecated, in favor of flags.fractional // flags in regexp.realNumber can be applied except exponent, eSigned. // assign default values to missing paramters flags = (typeof flags == "object") ? flags : {}; if(typeof flags.signed == "undefined"){ flags.signed = [true, false]; } if(typeof flags.symbol == "undefined"){ flags.symbol = "$"; } if(typeof flags.placement != "string"){ flags.placement = "before"; } if(typeof flags.signPlacement != "string"){ flags.signPlacement = "before"; } if(typeof flags.separator == "undefined"){ flags.separator = ","; } if(typeof flags.fractional == "undefined" && typeof flags.cents != "undefined"){ dojo.deprecated("dojo.regexp.currency: flags.cents", "use flags.fractional instead", "0.5"); flags.fractional = flags.cents; } if(typeof flags.decimal != "string"){ flags.decimal = "."; } // build sign RE var signRE = dojo.regexp.buildGroupRE(flags.signed, function(q){ if (q){ return "[-+]"; } return ""; } ); // build symbol RE var symbolRE = dojo.regexp.buildGroupRE(flags.symbol, function(symbol){ // escape all special characters return "\\s?" + symbol.replace( /([.$?*!=:|\\\/^])/g, "\\$1") + "\\s?"; } ); switch (flags.signPlacement){ case "before": symbolRE = signRE + symbolRE; break; case "after": symbolRE = symbolRE + signRE; break; } // number RE var flagsCopy = flags; //TODO: copy by value? flagsCopy.signed = false; flagsCopy.exponent = false; var numberRE = dojo.regexp.realNumber(flagsCopy); // build currency RE var currencyRE; switch (flags.placement){ case "before": currencyRE = symbolRE + numberRE; break; case "after": currencyRE = numberRE + symbolRE; break; } switch (flags.signPlacement){ case "around": currencyRE = "(" + currencyRE + "|" + "\\(" + currencyRE + "\\)" + ")"; break; case "begin": currencyRE = signRE + currencyRE; break; case "end": currencyRE = currencyRE + signRE; break; } return currencyRE; // String}dojo.regexp.us.state = function(/*Object?*/flags){ // summary: A regular expression to match US state and territory abbreviations // // flags An object. // flags.allowTerritories Allow Guam, Puerto Rico, etc. Default is true. // flags.allowMilitary Allow military 'states', e.g. Armed Forces Europe (AE). Default is true. // assign default values to missing paramters flags = (typeof flags == "object") ? flags : {}; if(typeof flags.allowTerritories != "boolean"){ flags.allowTerritories = true; } if(typeof flags.allowMilitary != "boolean"){ flags.allowMilitary = true; } // state RE var statesRE = "AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|" + "NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY"; // territories RE var territoriesRE = "AS|FM|GU|MH|MP|PW|PR|VI"; // military states RE var militaryRE = "AA|AE|AP"; // Build states and territories RE if(flags.allowTerritories){ statesRE += "|" + territoriesRE; } if(flags.allowMilitary){ statesRE += "|" + militaryRE; } return "(" + statesRE + ")"; // String}dojo.regexp.time = function(/*Object?*/flags){ // summary: Builds a regular expression to match any International format for time // description: The RE can match one format or one of multiple formats. // // Format // h 12 hour, no zero padding. // hh 12 hour, has leading zero. // H 24 hour, no zero padding. // HH 24 hour, has leading zero. // m minutes, no zero padding. // mm minutes, has leading zero. // s seconds, no zero padding. // ss seconds, has leading zero. // t am or pm, case insensitive. // All other characters must appear literally in the expression. // // Example // "h:m:s t" -> 2:5:33 PM // "HH:mm:ss" -> 14:05:33 // // flags: An object // flags.format A string or an array of strings. Default is "h:mm:ss t". // flags.amSymbol The symbol used for AM. Default is "AM". // flags.pmSymbol The symbol used for PM. Default is "PM". dojo.deprecated("dojo.regexp.time", "Use dojo.date.parse instead", "0.5"); // assign default values to missing paramters flags = (typeof flags == "object") ? flags : {}; if(typeof flags.format == "undefined"){ flags.format = "h:mm:ss t"; } if(typeof flags.amSymbol != "string"){ flags.amSymbol = "AM"; } if(typeof flags.pmSymbol != "string"){ flags.pmSymbol = "PM"; } // Converts a time format to a RE var timeRE = function(format){ // escape all special characters format = format.replace( /([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1"); var amRE = flags.amSymbol.replace( /([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1"); var pmRE = flags.pmSymbol.replace( /([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1"); // replace tokens with Regular Expressions format = format.replace("hh", "(0[1-9]|1[0-2])"); format = format.replace("h", "([1-9]|1[0-2])"); format = format.replace("HH", "([01][0-9]|2[0-3])"); format = format.replace("H", "([0-9]|1[0-9]|2[0-3])"); format = format.replace("mm", "([0-5][0-9])"); format = format.replace("m", "([1-5][0-9]|[0-9])"); format = format.replace("ss", "([0-5][0-9])"); format = format.replace("s", "([1-5][0-9]|[0-9])"); format = format.replace("t", "\\s?(" + amRE + "|" + pmRE + ")\\s?" ); return format; // String }; // build RE for multiple time formats return dojo.regexp.buildGroupRE(flags.format, timeRE); // String}dojo.regexp.numberFormat = function(/*Object?*/flags){ // summary: Builds a regular expression to match any sort of number based format // description: // Use this method for phone numbers, social security numbers, zip-codes, etc. // The RE can match one format or one of multiple formats. // // Format // # Stands for a digit, 0-9. // ? Stands for an optional digit, 0-9 or nothing. // All other characters must appear literally in the expression. // // Example // "(###) ###-####" -> (510) 542-9742 // "(###) ###-#### x#???" -> (510) 542-9742 x153 // "###-##-####" -> 506-82-1089 i.e. social security number // "#####-####" -> 98225-1649 i.e. zip code // // flags: An object // flags.format A string or an Array of strings for multiple formats. // assign default values to missing paramters flags = (typeof flags == "object") ? flags : {}; if(typeof flags.format == "undefined"){ flags.format = "###-###-####"; } // Converts a number format to RE. var digitRE = function(format){ // escape all special characters, except '?' format = format.replace( /([.$*!=:|{}\(\)\[\]\\\/^])/g, "\\$1"); // Now replace '?' with Regular Expression format = format.replace(/\?/g, "\\d?"); // replace # with Regular Expression format = format.replace(/#/g, "\\d"); return format; // String }; // build RE for multiple number formats return dojo.regexp.buildGroupRE(flags.format, digitRE); //String}dojo.regexp.buildGroupRE = function(/*value or Array of values*/a, /*Function(x) returns a regular expression as a String*/re){ // summary: Builds a regular expression that groups subexpressions // description: A utility function used by some of the RE generators. // The subexpressions are constructed by the function, re, in the second parameter. // re builds one subexpression for each elem in the array a, in the first parameter. // Returns a string for a regular expression that groups all the subexpressions. // // a: A single value or an array of values. // re: A function. Takes one parameter and converts it to a regular expression. // case 1: a is a single value. if(!(a instanceof Array)){ return re(a); // String } // case 2: a is an array var b = []; for (var i = 0; i < a.length; i++){ // convert each elem to a RE b.push(re(a[i])); } // join the REs as alternatives in a RE group. return "(" + b.join("|") + ")"; // String}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -