赞
踩
var reg = /\d/ //字面量创建
var inputObj = document.getElementById('user')
inputObj.onblur = function () {
console.log(reg.test(input.value))
}
var str = '2 jdjfjjs哈哈'
var reg = /\D/
console.log(reg.test(str))//true
var str = '前端_8A%$^%^&*'
var reg = new RegExp(/\w/)
console.log(reg.test(str))//true
var reg = new RegExp(/\W/)
console.log(reg.test('23A D__D汉字'))//true
var reg = new RegExp(/\s/)
console.log(reg.test('23AD__ D汉 字'))//true
var reg = /\S/
console.log(reg.test(' 5 '))//true
var reg = /./
console.log(reg.test('\n\n\n\n % \n\n'))//true
var reg = /^2/
console.log(reg.test('2iidfkjdf5454'))//true
var reg = /^ABC/
console.log(reg.test('ABCiidfkjdf5454'))//true
var reg = /^\d/ //至少有一个数字开头
console.log(reg.test('210ABCiidfkjdf5454'))//true
var reg = /^\S/ //开头至少有一个空白符
console.log(reg.test('210ABCiid fkjdf5454'))//true
var reg = /2$/
console.log(reg.test('2iidfkjdf54542'))//true
var reg = /^2$/
console.log(reg.test('2'))//true
var reg = /^\d$/
console.log(reg.test('6'))//true
*
重复零次或更多次\d*var reg = new RegExp(/^ab*$/)
console.log(reg.test('abbbbb'))//true
+
重复一次或更多次\d+var reg = new RegExp(/\d+/)
console.log(reg.test('a123213'))//true
var reg = new RegExp(/ab+/)
console.log(reg.test('bbbbbbbbaab'))//true
var reg = new RegExp(/^xy+/)
console.log(reg.test('xy前端9898前9前99'))//true
?
重复零次或一次console.log(reg.test('xy'))
{n}
至少重复n次var reg = /u{5}/
console.log(reg.test('xuuuuuy'))//连续5次
var reg = /\s{3}/
console.log(reg.test(' xuuusssuuy'))//以三个空格
^{n}$
必须重复n次var reg = /^u{5}$/
console.log(reg.test('uuuuu'))//true
{n,m}
重复n到m次var reg = /^A{2,7}$/
console.log(reg.test('AA'))//true
var reg = /^\d+$/
console.log(reg.test('044635432132132101'))//true
var reg = /[ab]/
console.log(reg.test('2545ab'))//true
var reg = /[0-9]/
console.log(reg.test('SDSD5823165722SD'))//true
// 匹配全数字
var reg = /^[0-9]+$/
// 匹配小数
var reg = /^\d+\.\d+$/
var reg = /^\d+[.]\d+$/
console.log(reg.test('5.36'))
// 匹配正负小数 5123.36 -5.36 +5.23
var reg = new RegExp(/^\d+\.\d+$|^[-]?[0-9]+\.[0-9]+$/)
console.log(reg.test('-93.89'))
var reg = /[a-z]/
var reg = /[A-Z]/
var reg = /[A-Za-z]/
console.log(reg.test('542SDSDrtrtSDSGR$TRGCV4'))
g:全局匹配 i:忽略大小写 gi:全局匹配+忽略大小写
var str = 'one 1005 2 tWO222 3 three 4fOur'
// 提取2
console.log(str.match('2')) //1个
console.log(str.match(/2/)) //1个
console.log(str.match(/2/g)) //['2', '2', '2', '2']
console.log(str.match(/o/gi)) //['o', 'O', 'O']
var str = 'one 1005 2 tWO222 3 three 4fOur'
//one 1AA5 2 tWO222 3 three 4fOur
console.log(str.replace('0', 'A').replace('0', 'A'))
console.log(str.replace(/0/g, 'A'))//one 1AA5 2 tWO222 3 three 4fOur
var reg = /\d/ //字面量创建
var reg = new RegExp(/\d/) //构造函数创建
console.log(localStorage.length)
setItem(key,value)
存储数据document.getElementById('save').onclick = function () {
localStorage.setItem('name', '小花')
localStorage.setItem('age', '18')
localStorage.setItem('sex', false)
console.log(localStorage)
}
getItem(key);
通过了key获取valuedocument.getElementById('get').onclick = function () {
console.log(localStorage.getItem('age'))
}
removeItem(key);
删除单个数据document.getElementById('remove').onclick = function () {
localStorage.removeItem('name')
console.log(localStorage)
}
clear()
删除所有数据document.getElementById('clear').onclick = function () {
localStorage.clear()
console.log(localStorage)
}
console.log(localStorage.key(0))
console.log(localStorage.key(1))
console.log(localStorage.key(2))
//---直接写 οnclick="add(); 下面就不用获取----------- <button id="add" onclick="add();">新增记录</button> <scsript> //获取元素 var result = document.getElementById("result")//输出表格或者文本 dataShow()//先调用 没有数据显示null // 2、新增记录 function add() { var siteName = document.getElementById("siteName")//网站名 var siteValue = document.getElementById("siteValue")//网址 localStorage.setItem(siteName.value, siteValue.value)//存储key value alert("添加成功!") console.log(localStorage); dataShow()//添加数据以后还要调用 显示到表格 } // 3、查找网站 function search() { var searchName = document.getElementById("searchName").value//网站名value值 var wangzhi = localStorage.getItem(searchName)//网址的value值 text.innerText = searchName + "的网址是:" + wangzhi //显示查出来的网址 } // 1、表格或者文字显示 function dataShow() { console.log(localStorage); if (localStorage.length == 0) {//如果没有数据 result.innerText = "数据为空......" } else { var str = "<table border='1'><tr><td>key</td><td>value</td></tr>" for (var i = 0; i < localStorage.length; i++) {//tr的个数与localStorage.length的个数一样 str += "<tr><td>" + localStorage.key(i) + "</td><td>"+localStorage.getItem(localStorage.key(i))+"</td></tr>" } str += "</table>" result.innerHTML=str//最后把str给result } } </script>
cookies
、sessionStorage
和localStorage
解释及区别?Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。