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

边栏推荐
- 第2章 还记得点、线、面吗(二)
- It is safer for individuals to choose which securities company to open an account for buying floor funds
- [practice] appium settings app is not running after 5000ms
- GCC简介
- Dataease installation upgrade
- 数据库系列:有什么办法对数据库的业务表进行无缝升级
- [monkey] Introduction to monkey test
- Wireshark数据抓包分析之FTP协议
- JS基础2
- Blue Bridge Cup Maze (dfs+ backtracking)
猜你喜欢

Katalon global variable is referenced in testobject

还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!

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

Industry analysis - quick intercom, building intercom

获取系统当前日期

Move command

Wealth management for programmers

Jetpack Compose Desktop 桌面版本的打包和发布应用

Metersphere implements UI automation elements that are not clickable (partially occluded)

2022中国信通院首届业务与应用安全发展论坛成功召开!
随机推荐
[cloud resident co creation] detailed introduction to DWS alarm service DMS and cluster connection mode
Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022
100 important knowledge points that SQL must master: retrieving data
Redis database
Making and using of static library
BigDecimal 类的 compareTo() 和 equals()方法
Mysql安装配置以及解决重装Mysql时忘记root password问题
Datetime and logging module
JS基础3
MySql5.7添加新用户
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
字符串 & 堆 & 方法区
Knowing the details of redis RDB, you can step on many holes less
Mongo database
如临现场的视觉感染力,NBA决赛直播还能这样看?
[Agora] get an Agora_ Example usage of refptr object
JS foundation 5
毕业季 新的开始
Oracle 日期格式化异常:无效数字
董宇辉,新东方以及凤凰卫视