详细介绍正则表达式

          正则是独立于编程语言的一个学科,用于解决模式匹配问题,Javascript提供了对于正则支持,此外,Java、c、python也都支持正则。正则能够应用在:检索,替换,爬虫,论文查重等领域。javascript

实例化正则表达式对象html

  1. 字面量
    var pattern = /正则表达式/标记
    var pattern = /abc/igm

  2. 构造函数
    var pattern = new RegExp(“正则表达式”,“标记”);
    var pattern = new RegExp(“abc”,“igm”);
    标记:
    i ignoreCase 忽略大小写
    g global 全局
    m multiline 多行
    u unicode 任何 Unicode 代码点的转义都会被解释。
    y sticky 属性反映了搜索是否具备粘性







正则表达式java

  1. 直接量
    abc 例如:/abc/ 查找目标串中是否含有abc
    python

  2. 字符类
    [abc] 例如:/[abc]/ 查找目标串中是否含有abc中任意一个字符
    [^abc] 例如:/[^abc]/ 查找目标串中是否含有除了abc以外任意一个字符
    [a-z] a~z中的任意一个字符
    \w 字母 [a-zA-Z0-9]
    \W 非字母 [^a-zA-Z0-9]
    \d 数字 [0-9]
    \D 非数字[^0-9]
    \s 空白符
    \S 非空白符








    web

    多行模式下
    ^ 以…开始 /^\d\w{3}\d$/ 觉得数字开头,以数字结尾
    $ 以…结尾

    正则表达式

  3. 数量词
    数量通常使用在子表达式(直接量,字符类,分组…)后
    /1[3578]\d{9}/
    {9} 重复9次
    {1,9} 重复1~9次
    {1,} 重复1次及以上
    {0,} 重复0次及以上
    * 等价于{0,}
    + 等价于{1,}
    ? 等价于{0,1}








    编程

    贪婪匹配
    默认是贪婪匹配
    {1,9} 优先匹配9次
    非贪婪匹配
    数量词后添加?就成为了非贪婪匹配
    {1,9}? 优先匹配1次




    数组

  4. 选择
    子表达式中间添加"|"表示选择
    例如:
    /hello|hi/


    编程语言

  5. 分组
    获取目标字符串中全部的url,而且分别拿到协议,ip,port,路径
    url 协议://ip:port/路径

    svn

var pattern =
/(http|https|ftp|svn) \ : //((\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})|(www.\w{2,}.com)):?(\d{2,5})(/[a-z0-9/]{2,})/ig
/() : //(()|()): ()()/

  1. 引用
    经过"\数字"对以前分组匹配结果的一种引用。\1 就表示对第一个分组匹配结果的引用

var str = “12hello12”
var str = “871wrold871”
var str = “871wrold888”

var pattern = /(\d{2,})\w+\1/

API

1.实例属性
RegExp.prototype.flags 标记
RegExp.prototype.source 正则字符串
RegExp.prototype.ignoreCase
RegExp.prototype.global
RegExp.prototype.multiline
RegExp.prototype.unicode
RegExp.prototype.sticky






这里有个简单的例子来直接的理解这些属性的做用:

var pattern = /hello/igm;
console.log(pattern);// /hello/gim

console.log("source:",pattern.source);// source: hello
console.log("flags:",pattern.flags);// flags: gim
console.log("ignoreCase:",pattern.ignoreCase);// ignoreCase: true
console.log("global:",pattern.global);// global: true
console.log("multiline:",pattern.multiline);// multiline: true
console.log("unicode:",pattern.unicode);// unicode: false

2.实例方法

RegExp.prototype.test(str)

目标字符串中是否能够知足正则表达式的匹配要求
支持全局匹配。当全局匹配的时候,会在正则表达式对象pattern上维 护一个变量 lastIndex,表示下次开始检索的位置。
参数:字符串
返回值:boolean


RegExp.prototype.exec(str)

从目标字符串中获取知足正则表达式匹配要求的子串。
支持全局匹配。当全局匹配的时候,会在正则表达式对象pattern上维护一个变量,lastIndex,表示下次开始检索的位置。
参数:字符串
返回值:数组
数组元素为匹配的结果
exec的返回值的数组中的第一个元素总体匹配的结果, 第二个元素为第一个分组结果, 第三个元素为第二个分组的结果
数组属性index表示当前子串在目标串中的位置,input表示目标串,groups表示分组





如下是一个检索网址的例子:

//检索网址
var str = "hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.2
0/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/p
ersonal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900";
//网址的正则表达式
var a = /(http|https|ftp|svn)\:\/\/((\d{ 1,3}\.\d{ 1,3}\.\d{ 1,3}\.\d{ 1,3})|(www\.\w{ 2,}\.com))\:?(\d{ 2,5})?(\/[a-z0-9/]{ 2,
})/ig
let result = null;
while(result = a.exec(str)){ 
    console.log(result);
}

输出的结果:

[
  'http://134.175.154.93:8888/personal/index',//总体的匹配结果
  'http',//第一个分组结果
  '134.175.154.93',
  '134.175.154.93',
  undefined,
  '8888',
  '/personal/index',
  index: 34,      //当前子串在目标串中的位置
  input: 'hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.20/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/personal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900',  //检索的目标串
  groups: undefined//分组
]
[
  'ftp://172.16.0.20/webui',
  'ftp',
  '172.16.0.20',
  '172.16.0.20',
  undefined,
  undefined,
  '/webui',
  index: 93,
  input: 'hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.20/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/personal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900',
  groups: undefined
]
[
  'http://www.larry.com/personal/my',
  'http',
  'www.larry.com',
  undefined,
  'www.larry.com',
  undefined,
  '/personal/my',
  index: 206,
  input: 'hello , i am terry, my website is http://134.175.154.93:8888/personal/index.html, ftp url is ftp://172.16.0.20/webui , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is http://www.larry.com/personal/my.index.html , his email is larry@briup. com, my other first is jacky, his telephoen is 17751229900',
  groups: undefined
]

总结 这篇是我在学习js正则时总结的一些基础,掌握!

相关文章
相关标签/搜索