JS字符串操作方法扩展string-extension.js
字符串操作方法扩展
/*
* 去掉字符串前后空格
*/
String.prototype.<span class="token function-variable function">trim = function () {
return this.replace(<span class="token regex">/(^\s*)|(\s*$)/g, "");
}
/*
* 把字符串转换为json
*/
String.prototype.<span class="token function-variable function">toJson = function () {
return $.parseJSON(this);
//return JSON.parse(this);
}
/*
* 把字符串转换为人民币的显示方式(如:¥123,456,78.12)
* @param decimalCount - 保留小数的位数
*/
String.prototype.<span class="token function-variable function">toMoneyString = function (decimalCount) {
var result = "";
var arr = this.split('.');
var i;
for (i = arr[<span class="token number">0].length - <span class="token number">3;i><span class="token number">0;i-=<span class="token number">3){
result = "," + arr[<span class="token number">0].substr(i, <span class="token number">3) + result;
}
result = "¥" + arr[<span class="token number">0].substring(<span class="token number">0, i+<span class="token number">3) + result;
if (arr.length > <span class="token number">1) {
if (decimalCount > arr[<span class="token number">1].length)
{
result += "." + arr[<span class="token number">1];
for (var j = <span class="token number">0; j < decimalCount - arr[<span class="token number">1].length; j++)
{
result += "0";
}
}
else
{
result += "." + arr[<span class="token number">1].substring(<span class="token number">0, decimalCount);
}
}
return result;
}
/*
* 获取字符串的字节数
*/
String.prototype.<span class="token function-variable function">getByteCount=function(){
return this.replace(<span class="token regex">/[^\x00-\xFF]/g, '**').length;
}
/*
* 切割字符串
* @param length - 切割的长度(按字符的字节数计算,比如一个中文算2个长度丶英文算1个长度)
*/
String.prototype.<span class="token function-variable function">cutString = function (length) {
var strCount = this.getByteCount();
if (strCount <= length) {
return str;
}
var sum = <span class="token number">0;
var cutString = "";
for (i = <span class="token number">0; i < this.length; i++) {
var c = this.charAt(i);
sum += c.getByteCount();
cutString += c;
if (sum >= length) {
break;
}
}
return cutString;
}
/*
* 移除字符串(如果移除的字符下标越界[startIndex+count>字符串自身长度],则不进行移除操作)
* @param startIndex - 开始移除的下标
* @param count - 要移除的字符个数
*/
String.prototype.<span class="token function-variable function">remove = function (startIndex, count) {
if (startIndex+count >= this.length) {
return this;
}
var str1 = this.substring(<span class="token number">0, startIndex);
var str2 = this.substring(startIndex + count, this.length);
return str1 + str2;
}
/*
* 移除字符串左边指定个数的字符(如果移除的字符数超出字符串本身的长度,则不进行移除操作)
* @param count - 要移除的字符个数
*/
String.prototype.<span class="token function-variable function">removeLeft = function (count) {
if (count == undefined) count = <span class="token number">1;
if (this.length >= count) {
return this.substring(count, this.length);
} else {
return this;
}
}
/*
* 移除字符串右边指定个数的字符(如果移除的字符数超出字符串本身的长度,则不进行移除操作)
* @param count - 要移除的字符个数
*/
String.prototype.<span class="token function-variable function">removeRight = function (count) {
if (count == undefined) count = <span class="token number">1;
if (this.length >= count) {
return this.substring(<span class="token number">0, this.length-count);
} else {
return this;
}
}
/*
* 把字符串转换为数字(整数或小数)
* @param defaultValue - 转换失败时返回的默认值
*/
String.prototype.<span class="token function-variable function">toNumber = function (defaultValue) {
if (isNaN(this)) {
return defaultValue;
} else {
return Number(this);
}
}
/*
* 把字符串转换为布尔类型
* @param defaultValue - 转换失败时返回的默认值
*/
String.prototype.<span class="token function-variable function">toBool = function (defaultValue) {
var str = this.toLowerCase();
if (str == "true") {
return <span class="token boolean">true;
} else if (str == "false") {
return <span class="token boolean">false;
} else {
return defaultValue;
}
}
/*
* 把字符串转换为日期类型
* @param defaultValue - 转换失败时返回的默认值
*/
String.prototype.<span class="token function-variable function">toDate = function (defaultValue) {
var str = this.replace(<span class="token regex">/-/g, "/");
var date = new <span class="token class-name">Date(str);
if (date == 'Invalid Date') {
return defaultValue;
} else {
return date;
}
}
/************************************ 数据校验 **************************************/
/*
* 自定义正则表达式验证
* @param pattern - 正则表达式
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isMatch=function (pattern) {
if (this == undefined || pattern == undefined) {
return <span class="token boolean">false;
}
return pattern.test(this);
}
/*
* 判断字符串为null或者为空字符串
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isNull = function () {
if (this == undefined) {
return <span class="token boolean">true;
}
if (this.trim() == "") {
return <span class="token boolean">true;
}
return <span class="token boolean">false;
}
/*
* 判断字符串不为null并且不为空字符串
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isNotNull = function () {
return !this.isNull();
}
/*
* 字符串长度验证
* @param field - 验证的字段
* @param minLength - 字符串允许最小的长度 - null表示不限制字符串的最小长度
* @param maxLength - 字符串允许最大的长度 - null表示不限制字符串的最大长度
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">compareLength = function (minLength, maxLength) {
if (minLength != null) {
if (this.length < minLength) {
return <span class="token boolean">false;
}
}
if (maxLength != null) {
if (this.length > maxLength) {
return <span class="token boolean">false;
}
}
return <span class="token boolean">true;
}
/*
* 整数格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isInt = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
return /^((\d)|([<span class="token number">1-<span class="token number">9]\d+))$/.test(this);
}
/*
* 小数格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isDecimal = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
return /^\d+\.\d+$/.test(this);
}
/*
* 整数或小数格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isNumber = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
return this.isInt()||this.isDecimal();
}
/*
* 数值大小验证
* @param field - 验证的字段
* @param minValue - 允许的最小值 - null表示不限制最小值
* @param maxValue - 允许的最大值 - null表示不限制最大值
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">compareValue = function (minValue, maxValue) {
var value = this.toNumber();
if (minValue != null) {
if (value < minValue) {
return <span class="token boolean">false;
}
}
if (maxValue != null) {
if (value > maxValue) {
return <span class="token boolean">false;
}
}
return <span class="token boolean">true;
}
/*
* 邮箱格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isEmail = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
//匹配邮箱,比如abc123@sina.com.cn或者1545113383@qq.com
return /^\w+@\w+(\.[a-zA-<span class="token constant">Z]{<span class="token number">2,<span class="token number">3}){<span class="token number">1,<span class="token number">2}$/.test(this);
}
/*
* 电话号码格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isPhone = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
//电话号码,区号3~4位,电话号码7~8位,分机号3~4位,区号和分机号可以忽略
return /^(\d{<span class="token number">3,<span class="token number">4}-)?\d{<span class="token number">7,<span class="token number">8}(-\d{<span class="token number">3,<span class="token number">4})?$/.test(this);
}
/*
* 手机号码格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isTelephone = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
//手机号码,目前手机号码都是从1开头的,一个11位数字
return /^<span class="token number">1\d{<span class="token number">10}$/.test(this);
}
/*
* 中文格式验证 (只要其中一个字符不为中文都会返回false)
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isChinese = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
return /^[\u4e00-\u9fa5]+$/.test(this);
}
/*
* 网址Url格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isUrl = function () {
if (this == undefined) {
return <span class="token boolean">false;
}
//网址可带任意个请求参数
var reg = <span class="token regex">/^http:\/\/(\S+\/?)*(\w+(\?\w+=[^&=\s]+(&\w+=[^&=\s]+)*)?)?$/;
return reg.test(this);
}
/*
* 日期格式验证
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">isDate = function () {
return this.toDate(<span class="token number">1) != <span class="token number">1;
}
/*
* 日期大小验证
* @param minDate - 日期允许的最小值 - null表示不限制日期的最小值(参数类型可以是字符串也可以是日期)
* @param maxValue - 日期允许的最大值 - null表示不限制日期的最大值 (参数类型可以是字符串也可以是日期)
* @return 验证成功返回true,失败返回false
*/
String.prototype.<span class="token function-variable function">compareDate = function (minValue,maxValue) {
var dateValue = this.toDate();
if (minValue != null) {
if (typeof minValue == "string") {
minValue=minValue.toDate();
}
if (dateValue < minValue) {
return <span class="token boolean">false;
}
}
if (maxValue != null) {
if (typeof maxValue == "string") {
maxValue = maxValue.toDate();
}
if (dateValue > maxValue) {
return <span class="token boolean">false;
}
}
return <span class="token boolean">true;
}