正则验证

构造函数正则

var exp = '/^1[3456789]\d{9}$/'

new RegExp(exp.slice(1,-1)).test(18086440878)

需要注意的是创建字符串变量时会把一个去掉,需要用两个\

找出一串字符串中出现(或者连续出现)最多的字符

function findSameStrs(s) {
 if(typeof s !== "string") return '';
 var str = s;
  //下面对乱的字符排序,如果题目要求”连续出现”最多字符的话,不用写
  str = s.split('').sort((a,b)=>a.localeCompare(b)).join('');

  var reg = /(w)1+/g;
  var arr = str.match(reg);//捕获匹配的字符
if(arr){
  // 将正则匹配到的结果继续按照字符串的长度排序(从大到小)
    arr.sort((a,b)=>b.length - a.length);
    console.log("出现次数最多的字符是:" + arr[0][0] + "出现:" + arr[0].length + "次")
  }else{
  // 如果正则没有匹配到的,则表示该字符串没有重复出现的字符,默认打印字符串第一位
    console.log("出现次数最多的字符是:" + s[0], "出现: 1次")
  }
}
onkeyup="this.value= this.value.match(/d+(.d{0,2})?/) ? this.value.match(/d+(.d{0,2})?/)[0] : ''"
onkeyup="this.value=this.value.replace(/D/g,'')"
onkeyup="this.value=this.value.replace(/D|^0/g,'')"
$("#Price").textbox('textbox').bind('keyup', function (e) {
$("#Price").textbox('setValue', $(this).val().match(/d+(.d{0,2})?/) ? $(this).val().match(/d+(.d{0,2})?/)[0] : '' );
});
原文地址:https://www.cnblogs.com/liufeiran/p/13427493.html