当前位置:网站首页>regular expression
regular expression
2022-07-08 01:30:00 【InfoQ】
RegExp - Regular expressions
Create a regular expression
// 1. Literal creation
const reg = /abcd/;
console.log(reg);
console.log(typeof reg);
// 2. Built in constructor creation
const reg2 = new RegExp('abcd');
console.log(reg2);
console.log(typeof reg2);
Two methods of regular expression
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);
Metacharacters of regular expressions - Basic metacharacter
1. \s
// Indicates that there needs to be one in the string Space character
const reg = /\s/ ;// Equivalent to / /
console.log(reg.test(' abcdefg'));
console.log(reg.test('abc d efg'));
// \S
// Indicates that there needs to be one in the string Non blank space character
const reg = /\S/
console.log(reg.test(' a'))
console.log(reg.test(' a a a '))
// 3. \t
// Indicates that there needs to be a... In your string tabs character
const reg = /\t/
console.log(reg.test(' ')) // Two spaces
console.log(reg.test(' ')) // A tab
// \d
// Indicates that there needs to be a... In your string Numbers character
const reg = /\d/
console.log(reg.test('abcdef'))
console.log(reg.test('ab1c1d111ef'))
// \D
// Indicates that there needs to be a... In your string The digital character
const reg = /\D/
console.log(reg.test('1234567890'))
console.log(reg.test('12a34a5a67a890'))
// \w
// Indicates that there needs to be a... In your string Numbers or letters or underscores character
const reg = /\w/
console.log(reg.test('[email protected]#$%$#@#$%$#$%$#$%$#'))
console.log(reg.test('[email protected]#123123$%$#@#$%$#$%asdasd$#$%$#'))
// \W
// Indicates that there needs to be a... In your string Alphanumeric underline Characters other than
const reg = /\W/
console.log(reg.test('asdasd123123123____'))
console.log(reg.test('asdasd123123123____%'))
console.log(reg.test('asdasd12312312##3____'))
// spot (.)
// Indicates that there needs to be a... In your string Non newline Content
const reg = /./
console.log(reg.test('\n'))
console.log(reg.test('a\nb'))
console.log(reg.test(' \n'))
// Oblique line (\)
// Indicates that there needs to be a letter in your string s
const reg = /s/
console.log(reg.test('abcd'))
console.log(reg.test('abcds'))
// Indicates that there needs to be a... In your string Space character
const reg2 = /\s/
console.log(reg2.test('abcdssss'))
console.log(reg2.test('abcdssss '))
// Indicates that there needs to be a... In your string Non newline Any character of
const reg3 = /./
console.log(reg3.test('asday'))
// Indicates that there needs to be a... In your string spot Text
const reg4 = /\./
console.log(reg4.test('asdasd'))
console.log(reg4.test('asdasd.'))
// Indicates that there needs to be a... In your string \ Text
const reg5 = /\\/
console.log(reg5.test('\\'))
边栏推荐
- QT -- package the program -- don't install qt- you can run it directly
- Frrouting BGP protocol learning
- Call (import) in Jupiter notebook ipynb . Py file
- Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
- Scalar / vector / matrix derivation method
- Kaptcha generates verification code on Web page
- Four digit nixie tube display multi digit timing
- Cs5212an design display to VGA HD adapter products | display to VGA Hd 1080p adapter products
- 2022 examination for safety production management personnel of hazardous chemical production units and new version of examination questions for safety production management personnel of hazardous chem
- Common effects of line chart
猜你喜欢
3. Multi agent reinforcement learning
About snake equation (5)
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
Four digit nixie tube display multi digit timing
Kuntai ch7511b scheme design | ch7511b design EDP to LVDS data | pin to pin replaces ch7511b circuit design
2022 safety officer-a certificate free examination questions and safety officer-a certificate mock examination
2022 operation certificate examination for main principals of hazardous chemical business units and main principals of hazardous chemical business units
USB type-C docking design | design USB type-C docking scheme | USB type-C docking circuit reference
2021-03-14 - play with generics
2021-04-12 - new features lambda expression and function functional interface programming
随机推荐
Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
Understanding of sidelobe cancellation
The Ministry of housing and urban rural development officially issued the technical standard for urban information model (CIM) basic platform, which will be implemented from June 1
Understanding of expectation, variance, covariance and correlation coefficient
LeetCode 练习——剑指 Offer 36. 二叉搜索树与双向链表
Basic realization of line graph
About snake equation (2)
3、多智能体强化学习
How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
Chapter IV decision tree
Recommend a document management tool Zotero | with tutorials and learning paths
Continued from the previous design
2021 welder (primary) examination skills and welder (primary) operation examination question bank
2022 safety officer-c certificate examination summary and safety officer-c certificate reexamination examination
NPM internal split module
4. Strategic Learning
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
The beauty of Mathematics -- the principle of fine Fourier transform
Scheme selection and scheme design of multifunctional docking station for type C to VGA HDMI audio and video launched by ange in Taiwan | scheme selection and scheme explanation of usb-c to VGA HDMI c