龙空技术网

详细介绍正则表达式

前端开发小白 214

前言:

现时各位老铁们对“js正则表达式ip”大概比较关心,我们都需要知道一些“js正则表达式ip”的相关知识。那么小编同时在网上汇集了一些关于“js正则表达式ip””的相关资讯,希望咱们能喜欢,咱们快快来了解一下吧!

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

实例化正则表达式对象

字面量

var pattern = /正则表达式/标记

var pattern = /abc/igm构造函数

var pattern = new RegExp(“正则表达式”,“标记”);

var pattern = new RegExp(“abc”,“igm”);

标记:

i ignoreCase 忽略大小写

g global 全局

m multiline 多行

u unicode 任何 Unicode 代码点的转义都会被解释。

y sticky 属性反映了搜索是否具有粘性

正则表达式

直接量

abc 例如:/abc/ 查找目标串中是否含有abc字符类

[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$/ 以为数字开头,以数字结尾

$ 以…结尾数量词

数量一般使用在子表达式(直接量,字符类,分组…)后

/1[3578]\d{9}/

{9} 重复9次

{1,9} 重复1~9次

{1,} 重复1次及以上

{0,} 重复0次及以上

* 等价于{0,}

+ 等价于{1,}

? 等价于{0,1}贪婪匹配

默认是贪婪匹配

{1,9} 优先匹配9次

非贪婪匹配

数量词后添加?就成为了非贪婪匹配

{1,9}? 优先匹配1次选择

表达式中间添加"|"表示选择

例如:

/hello|hi/分组

获取目标字符串中所有的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 就表示对第一个分组匹配结果的引用

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/gimconsole.log("source:",pattern.source);// source: helloconsole.log("flags:",pattern.flags);// flags: gimconsole.log("ignoreCase:",pattern.ignoreCase);// ignoreCase: trueconsole.log("global:",pattern.global);// global: trueconsole.log("multiline:",pattern.multiline);// multiline: trueconsole.log("unicode:",pattern.unicode);// unicode: false123456789

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

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 , ftp url is  , my phone 18812345432, my friend is larry ,his telephone is 13710009999,his web site is  , 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,})/iglet result = null;while(result = a.exec(str)){    console.log(result);}1234567891011

输出的结果:

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

总结

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

原文链接:

作者:wt1310445488

标签: #js正则表达式ip