当前位置:网站首页>正则表达式
正则表达式
2022-07-07 23:27:00 【InfoQ】
RegExp - 正则表达式
创建一个正则表达式
// 1. 字面量创建
const reg = /abcd/;
console.log(reg);
console.log(typeof reg);
// 2. 内置构造函数创建
const reg2 = new RegExp('abcd');
console.log(reg2);
console.log(typeof reg2);
正则表达式的两个方法
const reg = /abcd/;
// 1. test()
console.log(reg.test('aaabbbcccddd'));
console.log(reg.test('aaabcddddd'));
// 2. exec()
const res = reg.exec('aaabbbcccddd')
console.log(res);
const res2 = reg.exec('aaabcdddddabcdabcd');
console.log(res2);
正则表达式的元字符 - 基础元字符
1. \s
// 表示字符串里面需要有一个 空格 字符
const reg = /\s/ ;// 等价于 / /
console.log(reg.test(' abcdefg'));
console.log(reg.test('abc d efg'));
// \S
// 表示字符串里面需要有一个 非空格 字符
const reg = /\S/
console.log(reg.test(' a'))
console.log(reg.test(' a a a '))
// 3. \t
// 表示你的字符串里面需要有一个 制表符 字符
const reg = /\t/
console.log(reg.test(' ')) // 两个空格
console.log(reg.test(' ')) // 一个制表符
// \d
// 表示你的字符串里面需要有一个 数字 字符
const reg = /\d/
console.log(reg.test('abcdef'))
console.log(reg.test('ab1c1d111ef'))
// \D
// 表示你的字符串里面需要有一个 非数字 字符
const reg = /\D/
console.log(reg.test('1234567890'))
console.log(reg.test('12a34a5a67a890'))
// \w
// 表示你的字符串里面需要有一个 数字或者字母或者下划线 字符
const reg = /\w/
console.log(reg.test('[email protected]#$%$#@#$%$#$%$#$%$#'))
console.log(reg.test('[email protected]#123123$%$#@#$%$#$%asdasd$#$%$#'))
// \W
// 表示你的字符串里面需要有一个 数字字母下划线 以外的字符
const reg = /\W/
console.log(reg.test('asdasd123123123____'))
console.log(reg.test('asdasd123123123____%'))
console.log(reg.test('asdasd12312312##3____'))
// 点(.)
// 表示你的字符串里面需要有一个 非换行 内容
const reg = /./
console.log(reg.test('\n'))
console.log(reg.test('a\nb'))
console.log(reg.test(' \n'))
// 斜线(\)
// 表示你的字符串里面需要有一个字母 s
const reg = /s/
console.log(reg.test('abcd'))
console.log(reg.test('abcds'))
// 表示你的字符串里面需要有一个 空格字符
const reg2 = /\s/
console.log(reg2.test('abcdssss'))
console.log(reg2.test('abcdssss '))
// 表示你的字符串里面需要有一个 非换行 的任意字符
const reg3 = /./
console.log(reg3.test('asday'))
// 表示你的字符串里面需要有一个 点 文本
const reg4 = /\./
console.log(reg4.test('asdasd'))
console.log(reg4.test('asdasd.'))
// 表示你的字符串里面需要有一个 \ 文本
const reg5 = /\\/
console.log(reg5.test('\\'))
边栏推荐
- 2022 high voltage electrician examination skills and high voltage electrician reexamination examination
- General configuration title
- Know how to get the traffic password
- Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
- 4、策略学习
- Common configurations in rectangular coordinate system
- How to get the first and last days of a given month
- Call (import) in Jupiter notebook ipynb . Py file
- 2021 welder (primary) examination skills and welder (primary) operation examination question bank
- 4. Apprentissage stratégique
猜你喜欢
Complete model training routine
How to use education discounts to open Apple Music members for 5 yuan / month and realize member sharing
Problems of font legend and time scale display of MATLAB drawing coordinate axis
Su embedded training - Day9
redis的持久化方式-RDB和AOF 两种持久化机制
4. Strategic Learning
Blue Bridge Cup embedded (F103) -1 STM32 clock operation and led operation method
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
5、离散控制与连续控制
Recommend a document management tool mendely Reference Manager
随机推荐
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
String usage in C #
Gnuradio3.9.4 create OOT module instances
Basic realization of line chart (II)
Basic realization of line graph
Basic implementation of pie chart
2022 safety officer-c certificate examination summary and safety officer-c certificate reexamination examination
Multi purpose signal modulation generation system based on environmental optical signal detection and user-defined signal rules
Kafka-connect将Kafka数据同步到Mysql
Cs5212an design display to VGA HD adapter products | display to VGA Hd 1080p adapter products
Su embedded training - C language programming practice (implementation of address book)
Smart agricultural technology framework
The solution of frame dropping problem in gnuradio OFDM operation
Redis 主从复制
Leetcode notes No.7
10. CNN applied to handwritten digit recognition
Mathematical modeling -- knowledge map
2022 free test questions of fusion welding and thermal cutting and summary of fusion welding and thermal cutting examination
4. Strategic Learning
Cross modal semantic association alignment retrieval - image text matching