游戏屏蔽侮辱性词汇
<script>
var str ="我去,我草,你这游戏打的太牛逼了";
// 脏话 **;
var reg = /[去草]|牛逼/g
console.log(str.replace(reg,"*"))
</script>
input文本框去除两边空白
<script>
var str = " hello ";
var reg = /^\s+|\s+$/g;
var arr = [];
var res = str.replace(reg,"");
arr.push(res);
console.log(arr);
</script>
<script>
/* trim()去除字符串前后的空格
var str = " hello ";
var arr = [];
arr.push(str.trim());
console.log(arr);
</script>
小说网站爬虫过滤标签
<script>
/* 过滤标签*/
var str = "<p>我是</p>。<div>是</div>"
var reg =/<[^<>]+>/g;
console.log(str.replace(reg,""));
</script>
豆瓣网址获取数字段落
<script>
var str = "https://movie.douban.com/subject/1292052/";
var reg = /\d+/;
var res = str.match(reg);
console.log(res[0])
</script>
<script>
var str = "https://movie.douban.com/subject/1292052/";
var reg = /[^\d]/g
console.log(str.replace(reg,""))
</script>
豆瓣获取指定段落
<p id="app">
导演: 弗兰克·德拉邦特 Frank Darabont 主演: 蒂姆·罗宾斯 Tim Robbins /...<br>
1994 / 美国 / 犯罪 剧情
</p>
<script>
// var arr = [1994,"美国","犯罪 剧情"]
/* 1.获取内容,去除字符串前后尾的空格 */
var txt = document.getElementById("app").innerHTML.trim();
/* 2. 通过<br>标签分割字符串为数组,得到数组最后一项,去除前后位空格
3. 通过/分割字符为数组
*/
var arr = txt.split("<br>")[1].trim().split("/");
/* 4.去除数组中每一项 */
var reg = / /g
var res = arr.map(item=>item= item.replace(reg,""));
console.log(res)
</script>
邮箱验证
<input type="text" placeholder="请输入邮箱" id="email">
<script>
var email = document.getElementById("email");
email.onkeyup = function(event){
if(event.keyCode == 13){
var value = this.value.trim();
var reg = /^[\w]+@[0-9a-z]+\.[a-z]+$/;
console.log(reg.test(value));
}
}
/*邮箱的正则
395775347@qq.com
395775347@163.com
cheng_benchao@sina.com
chengg@gmail.com
fdfdj@xx.net
*/
/*
[\w]+@[0-9a-z]+\.[a-z]+
*/
</script>
密码验证
<input type="text" placeholder="请输入密码" id="email">
<script>
var email = document.getElementById("email");
email.onkeyup = function(event){
if(event.keyCode == 13){
var value = this.value.trim();
var reg = /^[a-zA-z]+\d{5,}$/
console.log(reg.test(value))
}
}
/* 请输入6位数密码
银行密码
var reg = /^\d{6}$/
*/
/*
cheng12345 --true
cheng123 --false
账户密码
以为字母开头,后面是数字,数字至少包含5位
var reg = /^[a-zA-z]+\d{5,}$/
*/
</script>
用户名验证
<input type="text" placeholder="请输入用户名" id="email">
<script>
var email = document.getElementById("email");
email.onkeyup = function(event){
if(event.keyCode == 13){
var value = this.value.trim();
var reg = /^[a-zA-Z]{1,8}$/
console.log(reg.test(value))
}
}
/*
用户名只能是字母,大小不限,不能超过8个字符
var reg = /^[a-zA-Z]{1,8}$/
*/
</script>