Home > Web Front-end > JS Tutorial > body text

js regular related use case sharing

php中世界最好的语言
Release: 2018-05-22 11:45:33
Original
1582 people have browsed it

这次给大家带来js正则相关使用案例分享,js正则相关使用的注意事项有哪些,下面就是实战案例,一起来看一下。

JS正则表达式一条龙讲解(从原理和语法到JS正则)

JS正则表达式大全(整理详细且实用)

vaScript动态正则表达式问题
请问正则表达式可以动态生成吗?
例如JavaScript中:
var str = "strTemp";
要生成:
var re = /strTemp/;
如果是字符连接:
var re = "/" + str + "/"即可

用js实现过滤script的正则

function stripscript(s) {
return s.replace(/.*?<\/script>/ig, '');
}
/之间的内容/ 是js正则语句的书写开始与结束
.*?是贪婪的匹配,如果不是贪婪的就是.*匹配任何字符,但用贪婪的就是不包含>的内容
/ig 是不区分大小写和全局替换

JS正则表达式的验证

//说明:除“XXX XX,XXX XX,XXX.00”格式外
//为上面提供各个JS验证方法提供.trim()属性 
String.prototype.trim=function(){ 
return this.replace(/(^\s*)|(\s*$)/g, "");
Copy after login

javascript 手机号码正则表达式验证函数

function checkMobile(){ 
 var sMobile = document.mobileform.mobile.value 
 if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(sMobile))){ 
  alert("不是完整的11位手机号或者正确的手机号前七位"); 
  document.mobileform.mobile.focus(); 
  return false; 
 } 
}
Copy after login

正则表达式创建方式的区别及编写简单的正则方式

在字面量方式中,我们//之间包起来的所有的内容都是元字符,有的具有特殊意义,大部分都是代表本身含义的普通的元字符

var name = 'wo';
var reg = /^\d+"+name+"\d+$/
Copy after login

为了解决上述想在正则里面加上一个变量这样的需求,我们只能使用实例创建的方式了

var reg = new RegExp("^\\d+"+name+"\\d+$","g")
Copy after login

js正则表达式实现数字每隔四位用空格分隔代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(document).ready(function () {
 $('#ant').on('keyup mouseout input', function () {
  var $this = $(this);
  var v = $this.val();
  /\S{5}/.test(v) && $this.val(v.replace(/\s/g, '').replace(/(.{4})/g, "$1 "));
 });
})
</script>
</head>
<body>
<input type="text" id="ant" />
</body>
</html>
Copy after login

javascript 正则表达式用法 小结

function checkreg(myreg,mytext) 
{ 
if(myreg.test(mytext) 
{ 
alert("ok"); 
return true; 
} 
else 
{ 
return false; 
} 
}
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

vue按钮多次点击重复提交数据如何处理

node.js 命令行工具使用详解

The above is the detailed content of js regular related use case sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!