当前位置:网站首页>day34 js笔记 正则表达式 2021.09.29
day34 js笔记 正则表达式 2021.09.29
2022-06-28 11:20:00 【即将成为大佬的小梦】
正则表达式(regular expression):做验证
创建方式:
- 直接创建(声明式、字面量方法) var reg = /表达式/ 常用的创建方式
- 构造函数方式 var reg = new RegExp() 相对麻烦一点
- 构造函数的简写方式 var reg = RegExp() 上一种new可以省略
正则表达式的方法:
1)验证: 正则表达式.test(字符串) --验证方法最后得到的结果是布尔类型(true/false)

2)提取:正则表达式.exec(字符串) --返回一个数组

3)查找:字符串.search(正则表达式) --符合就返回下标 不符合就返回-1

4)提取匹配:字符串.match(正则表达式) --返回数组 不符合就返回null

5)替换:字符串.replace(正则表达式,替换的内容)

正则常见的字符:
\d 任意一个数字 0~9
\D 任意一个非数字
\w 任意一个数字、字母、下划线
\W 任意一个非数字、字母、下划线
\s 一个空格
\S 一个非空格
. 一个字符
正则的修饰字符:提取符合条件的字符串
- {n} 表示字符会出现n次
- {n,m} 表示字符最少出现n次,最多m次
- {n,} 最少是n次,后面出现多少次没有限制 匹配到返回数组 匹配不到返回null

正则的常见特殊符号修饰(元字符):
- + 最少有一位,最多不限制
- ? 最多有一位,或者可以没有(没有匹配到就返回空数组) - 不加符号的时候返回的是null
- * 所有位都可以
- .* 万能匹配

首尾修饰字符:
- ^ 开头
- $ 结尾

取反修饰符:[^]

或者修饰符: |

匹配中文汉字:中文汉字对应的unicode码: [\u4e00-\u9fa5]

模式修正符:
全局匹配/贪婪匹配 - 只需要在当前的正则表达式的后面加一个字母 'g'

--不考虑大小写时 在正则表达式后面添加一个i

边栏推荐
猜你喜欢

Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022

Get current system date

Making and using of dynamic library (shared library)

数据库系列:有什么办法对数据库的业务表进行无缝升级

New listing of operation light 3.0 - a sincere work of self subversion across the times!

阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别

Dataease installation upgrade

Scientific research - web of science retrieval skills

How to use output in katalon

Word、PDF、TXT文件实现全文内容检索需要用什么方法?
随机推荐
Graduated
Introduction to GDB
For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
Debug debugging in katalon
[monkey] Introduction to monkey test
SQL必需掌握的100个重要知识点:检索数据
New listing of operation light 3.0 - a sincere work of self subversion across the times!
李宏毅《机器学习》丨7. Conclusion(总结)
Metersphere uses JS to refresh the current page
Industry analysis - quick intercom, building intercom
Convert the file URL in the browser to a file stream
Basic 02: variable, remember the mobile number of the object
Machine learning project captcha based on verification code recognition_ Trainer operation practice
JS基础1-JS引入与运算符
Redis6 一:Nosql引入、Redis可以解决什么问题?
MySQL (III)
When an entity is converted to JSON, the field with null value is lost
时间戳和date转换「建议收藏」
Yann Lecun's new paper: the road to building automatic agents
[practice] appium settings app is not running after 5000ms