介绍正则表达式 (ip地址正则表达式)

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

实例化正则表达式对象

  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 属性反映了搜索是否具有粘性

正则表达式

  1. 直接量abc 例如:/abc/ 查找目标串中是否含有abc
  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 非空白符多行模式下^ 以…开始 /^\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/路径

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.ignoreCaseRegExp.prototype.globalRegExp.prototype.multilineRegExp.prototype.unicodeRegExp.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
123456789

对了,在这里说一下,我目前是在职web前端开发,如果你现在正在学习前端,了解前端,渴望成为一名合格的web前端开发工程师,在入门学习前端的过程当中有遇见任何关于学习方法,学习路线,学习效率等方面的问题,都可以随时关注并私信我:前端,我都会根据大家的问题给出针对性的建议,缺乏基础入门的视频教程也可以直接来找我,我这边有最新的web前端基础精讲视频教程, 还有我做web前端技术这段时间整理的一些学习手册,面试题,开发工具,PDF文档书籍教程,都可以直接分享给大家。

2.实例方法

RegExp.prototype.test(str)

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

RegExp.prototype*ex.e**c(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*ex.e**c(str)){
    console.log(result);
}
1234567891011

输出的结果:

[
  '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
]
123456789101112131415161718192021222324252627282930313233343536

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

正则表达式最详细讲解,ip地址正则表达式

原文链接:https://blog.csdn.net/wt1310445488/article/details/108939299?utm_medium=distribute.pc_category.none-task-blog-hot-5.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-5.nonecase&request_id=

作者:wt1310445488