当前位置:网站首页>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

边栏推荐
- soapui的菜鸟教程
- Which broker is safer and more convenient to open an account for Oriental Fortune mobile stock?
- Spatial-Temporal时间序列预测建模方法汇总
- 人人都可以参与开源!龙蜥社区最不容错过的开发者活动来了
- 毕业季,给初入社会的你一些建议
- 100 important knowledge points that SQL must master: retrieving data
- TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
- 《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
- Wireshark数据抓包分析之FTP协议
- Debug debugging in katalon
猜你喜欢

静态库的制作和使用

Yann Lecun's new paper: the road to building automatic agents

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

Remote connection of raspberry pie in VNC viewer mode without display

Dataease installation upgrade

Scientific research - web of science retrieval skills

JS foundation 1-js introduction and operator

2022中国信通院首届业务与应用安全发展论坛成功召开!

Get current system date

Katalon framework tests web (XX) custom keywords and upload pop-up operations
随机推荐
This Exception was thrown from a job compiled with Burst, which has limited exception support. 报错
[practice] appium settings app is not running after 5000ms
2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
GDB简介
Lihongyi, machine learning 7 Conclusion
Blackmail virus of industrial control security
JS foundation 1-js introduction and operator
Zero foundation self-study SQL course | if function
Word、PDF、TXT文件实现全文内容检索需要用什么方法?
使用ssm项目对Mysql8进行访问的时候,出现连接失败和一些错误的解决办法
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
Redis6 1: what problems can be solved by the introduction of NoSQL and redis?
[semidrive source code analysis] [x9 chip startup process] 32 - play module analysis - RTOS side
Calculate time using calendar
Jetpack Compose Desktop 桌面版本的打包和发布应用
使用API快捷创建ECS
毕业了
Summary of spatial temporal time series prediction modeling methods
Create ECS using API shortcut
无法重新声明块范围变量