A complete collection of regular expressions in js
这次给大家带来js中的正则表达式大全,在js中使用正则表达式的注意事项有哪些,下面就是实战案例,一起来看一下。
1、什么是正则?
正则也叫做规则,让计算机能够读懂人类的规则(正则都是操作字符串的)
2、什么是正则表达式?
正则表达式是由一个字符序列形成的搜索模式。
当你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容。
正则表达式可以是一个简单的字符,或一个更复杂的模式。
正则表达式可用于所有文本搜索和文本替换的操作。
3、正则的写法
var re = /a/; 正则的简写,其中a是字符串。如果写作 var re = /'a'/;此种写法是错误的
var re = new RegExp('a'); 正则表达式的全写(除了必须要用全写的形式一般建议用简写)注意:当正则需要传参的时候一定要用全称的写法
正则中的常用转义字符
\ s : 空 格 \ S : 非空格
\ d : 数 字 \ D : 非数字
\w : 字 符 \ W : 非字符
//在正则中,数字,字母,下划线统统都是字符
. : 代表任意字符 \ . : 代表真正的点
\ b :独立部分 \ B : 非独立的部分
正则表达式中的量词
{4,7} :最少出现4次,最多出现7次 {4,} :最少出现4次
{4} :正好出现4次 + :至少出现1次,是{1,}的简写
? :0次或1次{0,1} * :至少出现0次{0,}
^ :放正则的最开始位置,就代表起始的位置 $ : 放正则的最后位置,就代表结束的意思
正则中的默认是区分大小写的,如果想要不区分大小写,在正则的最后加标识 i var re = /B/i;
正则默认:正则匹配成功就会结束,不会继续匹配,如果想要全部查找,就要加标识g(全局匹配)。
| :在正则表达式中表示‘或’的意思
正则表达式的字符类:一组相似的字符 [ ]中括号的整体代表一个字符
排除:^ 前面我们已经知道,如果^放在正则的最开始位置,就代表起始的位置,那么如果^放在[ ]里面的话,就代表排除的意思
范围:[a-z] : a~z 的26个字符,整体只代表一位
var str = 'abc'; var re = /a[^bde]c/; alert(re.test(str))//false var str = 'abc'; var re = /a[bde]/; alert(re.test(str));// true \ 数字:重复子项 \ 1:重复的第一个子项 \ 2:重复的第二个子项 [html] view plain copy var str = 'abca'; var re = /(a)(b)(c)\1/; alert(re.test(str));//true [html] view plain copy var str1 = 'c9'; var str2 'cc'; alert(/\w\w/.test(str1));//true alert(/(\w)\1/.test(str2));//true
5、正则表达式的常用方法
test、search、mach、replace
test:正则去匹配字符串,如果匹配成功就返回真,如果匹配失败就返回假
test的写法:正则.text(字符串);
例:
var str1 = 'abcdef'; var re = /b/; alert(re.test(str)); //true
search:正则去匹配字符串,如果匹配成功,就返回匹配成功的位置,如果匹配失败就返回-1
说明
search() 方法不执行全局匹配,它将忽略标志 g。它同时忽略 regexp 的 lastIndex 属性,并且总是从字符串的开始进行检索,这意味着它总是返回 stringObject 的第一个匹配的位置。
search的写法:字符串.search(正则);
例:
var str2 = 'abcde'; var re = /b/; alert(str.search(re)); // 1
match:正则去匹配字符串,如果匹配成功,就返回匹配成功的数组,如果匹配不成功就返回null。
说明:这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g。
match的写法:字符串.match(正则);
例:
var str = ‘haj123sdk443nas33kdjalsd879’; var re = /\d/g; alert(str.match(re)); //[1,2,3,4,4,3,3,3,8,7,9]
例:
var re = /\d\d/g; alert(str.match(re)); // [12,44,33,87];
replace:正则去匹配字符串,匹配成功的字符去替换成新的字符串,replace的第二个参数可以是字符串,也可以是一个回调函数。
replace的写法:字符串.replace(正则,新的字符串); 或者为 :字符串.replace(正则,回调函数);
例:
var str = 'aaa'; var re = /a/; str = str.replace(re,'b'); alert(str); //baa
第一个参数是正则,第二个参数是带$符的字符串
var str3 = '这是一段原始文本,"3c这要替换4d"!'; var newStr = str3.replace( /([0-9])([a-z])/g,"$1" ); console.log( newStr ); //输出: 这是一段原始文本,"3这要替换4"!';
注:此例子为引用点击打开链接
上面的例子是当replace的第二个参数是字符串时,相对来说是比较简单的,那么接下来我们就一起看看当replace的第二个参数是回调函数时的情况。那么在写之前我们先来了解一下正则中的匹配子项
正则中的匹配子项
匹配子项:小括号()。小括号还有另外一个意思,即分组操作
把正则中的整体叫做‘母亲’,然后把左边第一个小括号里面的正则叫第一个子项(母亲的第一个孩子),第二个小括号就是第二个子项,以此类推
注:下面的两个例子为引用点击打开链接
例:1)、回调函数只有一个参数的时候,则函数的参数为匹配的正则的整体
var str4 = '这是一段原始文本,需要替换的内容"aa这要bbb替换ccccc"!'; var newStr = str4.replace( /[a-z]+/g,function ($0){ var str = ''; for (var i = 0; i < $0.length; i++) { str += '*'; }; return str; } ); console.log( newStr ); //这是一段原始文本,需要替换的内容"**这要***替换*****"!
2)、回调函数有多个参数的时候,且看如下例题
var str5 = '这是一段原始文本,需要替换的内容"3c这要替换4d"!'; var newStr = str5.replace( /([0-9])([a-z])/g,function (arg1,arg2,arg3,arg4,arg5){ console.log( arg1 ); console.log( arg2 ); console.log( arg3 ); console.log( arg4 ); console.log( arg5 ); } ); //输出: 3c 3 c 17 这是一段原始文本,需要替换的内容"3c这要替换4d"! 4d 4 d 23 这是一段原始文本,需要替换的内容"3c这要替换4d"!
上面的例子第一个参数arg1表示匹配的整体,arg2表示第一个子表达式,arg3表示第二个子表达式,接下来的参数arg4是一个整数,声明了表示子匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。
以上就是我对正则表达式知识的整理,具体实例也在整理中
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
The above is the detailed content of A complete collection of regular expressions in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
