regular expressions

regular expressions

参考博客:https://blog.csdn.net/zhouzhaoxiong1227/article/details/52026323?utm_source=blogxgwz1

1.数字

1)正整数: ^[1-9][0-9]*$

2)非正整数: ^((-[1-9][0-9]*)|(0))$

3)负整数:^-[1-9][0-9]*$

4)整数: ^(0|-?[1-9][0-9]*)$

5)非负浮点数:^\d+(\.\d+)?$

2.字母

1)英文字符串:^[A-Za-z]+$

2)英文大写串:^[A-Z]+$

3)英文小写串:^[a-z]+$

4)英文字符数字串:^[A-Za-z0-9]+$

5)英文数字加下划线串:^\w+$

3.常见

1.E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$

2.URL:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\‘:+!]*([^<>\"\"])*$

3.邮政编码:^[1-9]\d{5}$

4.中文:^[\u4e00-\u9fA5]+$

5.电话号码:^((\d2,3\d2,3)|(\d{3}\-))?(0\d2,30\d2,3|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$

6.手机号码:^1\d{10}$   //  ^1[345789]\d{9}$

7.首尾空格:(^\s+)|(\s+$)

8.身份证:^(\d{15}|\d{18})$      (注:中国的身份证为15位或18位)

9.账号:^[a-zA-Z]\w{4,15}$      (注:字母开头,允许5-16字节,允许字母数字下划线)

10.IP:^([1-9]\d{0,1}|1\d{2}|2[0-4]\d|25[0-5])(\.([1-9]\d{0,1}|1\d{2}|2[0-4]\d|25[0-5])){3}$   (IP是由大于等于0且小于等于255的数字、“.”组成的,验证每个数字分项再和“.”拼接就可以了)

测试代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>手机号码匹配</title>
</head>
<body>
    手机:<input type="text" id="phone">

    <script type="text/javascript">
        //获取输入的手机号
        var phone = document.getElementById(‘phone‘);
        //手机号输入框失去焦点
        phone.onblur = function(){
            var tv = this.value;
            var reg = /^1[345789]\d{9}$/;
            if (reg.test(tv)){
                alert(‘Yes‘)
            }else{
                alert(‘No‘)
            }
        }
    </script>
</body>
<html>

原文地址:https://www.cnblogs.com/CheeseIce/p/9866022.html

时间: 2024-10-08 11:13:42

regular expressions的相关文章

[转]8 Regular Expressions You Should Know

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "s

Using Regular Expressions in Python

1. 反斜杠的困扰(The Backslash) 有时候需要匹配的文本带有'\',如'\python',因为正则表达式有些特殊字符有特殊意义,所以需要前面加上'\'来消除特殊意义,这里匹配的正则表达式是'\\python',这时候如果要编译这个正则表达式需要re.compile('\\\\python'),因为在传递字符串的时候python本身就需要用'\\'来表示'\',也就造成了反斜杠的泛滥. 使用前缀'r'可以解决这个问题:’r'之后的'\'只是这个字符本身而没有特殊意义,比如r'\n'表

PCRE Perl Compatible Regular Expressions Learning

catalog 1. PCRE Introduction 2. pcre2api 3. pcre2jit 4. PCRE Programing 1. PCRE Introduction The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5. PCRE has its own nat

8 Regular Expressions You Should Know

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "s

Finding Comments in Source Code Using Regular Expressions

Many text editors have advanced find (and replace) features. When I’m programming, I like to use an editor with regular expression search and replace. This feature is allows one to find text based on complex patterns rather than based just on literal

Eloquent JavaScript #09# Regular Expressions

索引 Notes js创建正则表达式的两种方式 js正则匹配方式(1) 字符集合 重复匹配 分组(子表达式) js正则匹配方式(2) The Date class 匹配整个字符串 Choice patterns 正则匹配的机制 回溯Backtracking Replace 贪婪匹配Greed 动态构建正则表达式 Search The lastIndex property 遍历匹配项 解析INI文件 国际字符 Excercise Regexp golf Quoting style Numbers

Python re module (regular expressions)

regular expressions (RE) 简介 re模块是python中处理正在表达式的一个模块 1 r"""Support for regular expressions (RE). 2 3 This module provides regular expression matching operations similar to 4 those found in Perl. It supports both 8-bit and Unicode strings; b

[Regular Expressions] Find Repeated Patterns

Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum and maximum lengths. In this lesson we'll use Regular Expression Quantifiers to match repeated patterns, common Quantifier patterns, and using shorthand

[label][翻译][JavaScript Regular Expression]JavaScript Regular Expressions

原文:http://www.javascriptkit.com/javatutors/re.shtml 校验用户的输入是每一个软件开发者的必须要做的事情. 正则表达式与模式 如何在JavaScript中使用正则表达式呢?这里有两种方式: 1)字面量语法. 2)当你需要动态构建正则表达式时,可以通过RegExp()构造函数. 字面量语法如下: var RegularExpression = /pattern/; RegExp()构造函数方法如下: var RegularExpression = n