当前位置:网站首页>正则表达式
正则表达式
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('\\'))
边栏推荐
- Ag7120 and ag7220 explain the driving scheme of HDMI signal extension amplifier | ag7120 and ag7220 design HDMI signal extension amplifier circuit reference
- 133. Clone map
- 2022 safety officer-c certificate examination paper and safety officer-c certificate simulated examination question bank
- Su embedded training - Day8
- Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
- The solution of frame dropping problem in gnuradio OFDM operation
- Understanding of expectation, variance, covariance and correlation coefficient
- Solve the error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- 2022 high voltage electrician examination skills and high voltage electrician reexamination examination
- Su embedded training - Day5
猜你喜欢
Markdown learning (entry level)
Transportation, new infrastructure and smart highway
7. Regularization application
General configuration title
How to use education discounts to open Apple Music members for 5 yuan / month and realize member sharing
Getting started STM32 -- how to learn stm32
Understanding of prior probability, posterior probability and Bayesian formula
Chapter VIII integrated learning
The examination contents of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge) in 2021 and the free examination questions of the third batch of Guangdong Prov
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
随机推荐
Vscode reading Notepad Chinese display garbled code
Kindle operation: transfer downloaded books and change book cover
npm 内部拆分模块
[loss function] entropy / relative entropy / cross entropy
Share a latex online editor | with latex common templates
133. Clone map
FIR filter of IQ signal after AD phase discrimination
The combination of relay and led small night light realizes the control of small night light cycle on and off
Ag9310meq ag9310mfq angle two USB type C to HDMI audio and video data conversion function chips parameter difference and design circuit reference
USB type-C docking design | design USB type-C docking scheme | USB type-C docking circuit reference
Call (import) in Jupiter notebook ipynb . Py file
General configuration tooltip
解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
5. Over fitting, dropout, regularization
Y59. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (III, II)
Common configurations in rectangular coordinate system
2022 safety officer-c certificate examination paper and safety officer-c certificate simulated examination question bank
2021-03-06 - play with the application of reflection in the framework
Markdown learning (entry level)
Connect to the previous chapter of the circuit to improve the material draft