javascript 脚本源码

名称 描述
is_date( dtstr ) 判断字符串是否是日期, 日期分隔符可用 "-", "/", "."
Trim, LTrim, RTrim() 字符串去除头尾空格功能, 集成到 String 类后直接调用

is_date

function is_date( dtstr )
{
  var y, m, d;
  var arr, dt;
  arr = /^(\d{4})([-/.])(\d{1,2})(\2)(\d{1,2})$/.exec(dtstr);
  if( arr )
  {
    dt = new Date( arr[1], arr[2], arr[3] );
    y = parseInt(arr[1],10);
    m = parseInt(arr[3],10);
    d = parseInt(arr[5],10);

    dt = new Date(y,m-1,d);

    if( dt.getFullYear()==y && dt.getMonth()+1==m && dt.getDate()==d )
      return true;
  }
  return false;
}

Trim LTrim RTrim

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, "");
}