当前位置:网站首页>JS foundation -- regular expression
JS foundation -- regular expression
2022-07-25 02:48:00 【H5_ ljy】
List of articles
One 、 What is regular expression ?
Regular expressions are sequences of characters that make up a search pattern .
When searching for data in text , Search patterns can be used to describe the content of the search .
Regular expressions can be single characters , Or more complex patterns .
Regular expressions can be used to perform all types of text search and text substitution operations .
for example :
/Ljy/i Is a regular expression .
Ljy Is the mode ( Use... In search ).
i It's a modifier ( Change the search to case insensitive ).
Two 、 Regular expression modifiers
| Modifier | describe |
|---|---|
| i | Perform a match that is not case sensitive . |
| g | Perform a global match ( Find all matches instead of stopping after finding the first one ). |
| m | Perform multi line matching . |
3、 ... and 、 Regular expression patterns
Parentheses are used to find a range of strings :
| expression | describe |
|---|---|
| [abc] | Look for any characters between brackets . |
| [0-9] | Find anything from 0 to 9 The number of . |
| (x|y) | Look up the information from |
Metacharacters are characters with special meanings :
| Metacharacters | describe |
|---|---|
| \d | Find number . |
| \s | Look for blank characters . |
| \b | Match word boundaries . |
| \uxxxx | Find in hexadecimal numbers xxxx Stipulated Unicode character . |
Quantifiers Define quantifiers :
| quantifiers | describe |
|---|---|
| n+ | Match any containing at least one n String . |
| n* | Match any containing zero or more n String . |
| n? | Match any containing zero or one n String . |
Four 、 How to use regular expressions ?
Methods in strings
search()
search() Method uses an expression to search for a match , Then return to the matching position .
var str = "ljy666";
console.log(str.search(/666/g))
z
replace()
replace() Method returns the modified string where the pattern is replaced .
var str = "ljy666";
console.log(str.replace(/666/g,999))
match
match() Method to retrieve the specified value in a string , Or find a match for one or more regular expressions .
Return value : An array of matching results . The contents of the array depend on regexp Is there a global flag g.
var str = "ljy666j88j";
var reg=/j/g
console.log(str.match(reg))

split
split() Method is used to split a string into an array of strings . If the regular expression passed in , Then it is divided according to the character matched by the regular expression .
var str = "ljy666j88j";
var reg=/[6-8]/g
console.log(str.split(reg))

Regular expression methods
test()
It searches for strings through patterns , And then, based on the results, it returns true or false.
var str = "ljy666";
var reg=/j/g
console.log(reg.test(str))

exec()
It's through a specified pattern (pattern) Search string , And return the found text .
If no match is found , Then return to null.
var str = "ljy666";
var reg=/j/g
console.log(reg.exec(str))

5、 ... and 、 Case study
1. Determine whether the file is JPG perhaps PNG picture .jpg perhaps .png ending
var reg=/\.(jpg|png)$/ //.jpg perhaps .png ending
var str="ljy.js"
var re=reg.test(str)
console.log(re)

2. Remove the leading and trailing spaces of the string
var reg=/^\s/g
var str=" ab c "
var re=str.replace(reg,"")
console.log("---"+re+"--")
3. Determine whether it is a mobile phone number
var reg=/^1[3|5|6|7|8|9][0-9]{9}/
var str=18888888888
console.log(reg.test(str))

边栏推荐
- Introduction to web security telent testing and defense
- Text reading end judgment
- Vite dynamically loads static resource pictures, and fixes the 404 problem of pictures after packaging.
- Keil compile download error: no algorithm found for: 08000000h - 08001233h solution
- Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil
- Permanently mount the image steps
- @Retryable @backoff @recover retry the use of annotations
- Go common standard library -time
- [C language] program compilation (preprocessing)
- Mid year summary and personal feelings
猜你喜欢
![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/26/18fd3106f887f503479f081e29dac3.jpg)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience

Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)

On Calc optimization of calcite

Object.defineproperty use

Strategy mode, just read one article

Rolling division, Young's matrix and three-step flip

English grammar_ Reflexive pronoun

Pypi counts the number of Downloads

Technical experts from large factories: Thoughts on cloud nativity and software supply chain security

The latest interview questions and analysis of software testing in 2022
随机推荐
Generator set work arrangement problem code
Three ways to solve your performance management problems
"Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
【C】 Advanced knowledge of file operation
YouTube Download and (batch) Download
Vite dynamically loads static resource pictures, and fixes the 404 problem of pictures after packaging.
Several dpdk control frameworks
Selenium framework operation steelth.min.js file hides browser fingerprint features
How to communicate with aliens
2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.
Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
Dynamic programming -- Digital DP
Vulntarget vulnerability shooting range -vulntarget-b
YuQue - a useful tool for document writing and knowledge precipitation
ICO objects in classification
Consul cluster deployment
Summary and sorting of XSS (cross site script attack) related content
Study notes of filebeat
Go common standard library -time
Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil