Regular expression js is an object
by the following code:
rer.test(str) Verify whether the string conforms to the reg rules whether there is a rer regular expression in the str string
str: string rer: regular expression
var rer = /\./ //just point var reg = /Huaqing/ var str = "Summer Solstice Bullfrog 666." var res = rer.test(str) //Verify that the string conforms to the reg rule if there is a rer regular expression in the str string console.log(res)
Another usage of the test function:
new RegExp creates a regular object
var reg1 = new RegExp("summer solstice winter solstice") var str1 = "summer solstice winter solstice" var res = reg1.test(str1) console.log(res);
match
by the following code:
str2.match(reg2) : str2 is a string, reg2 is a regular expression to be matched in a string, opposite to test
/[a-c][a-c]/ig means to take two numbers. The first is the number between a-c and the second is also between a-c. The two numbers should be adjacent
i means case insensitive g means unlimited number
When reg2 is changed to /a/ match, it will be returned as an array, and use str.length to print the number of occurrences of a
var reg2 = /[a-c][a-c]/ig //Take out two bits, the first is between a and c, and the second is also var str2 = "bcdeAfgAaa666" var arr = str2.match(reg2) //Return the first value that matches, add g to return multiple console.log(arr);//bc Aa
Determine if it starts or ends with...
/^a/ Does it start with a /a$/ Does it end with $
Use the test function to judge
var reg3 = /^a/i //a$ ends with a ^a starts with a var str3 = "abcdeAfgAaa666A" var arr1 = reg3.test(str3) console.log(arr1);
Finds that end with:
/\.(jpg|png)$/ /. means . (jpg|png---jpg or png $---ends with ..
var reg4 = /\.(jpg|png)$/ //Find files ending in .jpg or .png var str4 = "qabAcdefAg6acbb66a.jpg" var re = str4.match(reg4) console.log(re);
one bit per character
[Zhao Qiancun][Cloud Crane][abc] Take one digit in each bracket and combine it
^[Zhao Qiancun][Cloud Crane][abc]Starting with a word in Zhao Qiancun
var reg5 = /[Zhao Qiancun][Yunhe][abc]/g //Take one digit for each square bracket var str5="Zhao He a Zhao Yun bc Murazuru a" var arr3=str5.match(reg5) console.log(arr3);//'Zhao He a', 'Zhao Yun b', 'Mura Crane a']
Written question: remove trailing spaces
/(^\s+)|(\s+$)/g \s means space here means the beginning of a space or the end of a space
re=str.replace(reg,"") replace Replace the reg (starting space or trailing space) in the str character with ""null
// Remove leading and trailing spaces from a string of written test questions // str.tirm() var reg=/(^\s+)|(\s+$)/g //remove trailing spaces var str=" abc " var re=str.replace(reg,"")//remove spaces in str console.log(re);
/[a]+/ Multiple a
=/[a-b]+/ Multiple occurrences of a and b are fine
/a{3}/ Three connected together
Regular phone number
var reg=/^1[3||5||7||8||9][0-9]{9}/ //1 starts with 35789 as the second digit followed by nine random numbers var str="18623481834" console.log(reg.test(str))
exec function
The nature and match are almost the same as returning the value that matches the regular expression, but the position of the regular expression of the string is opposite and the same as test
The second run will start looking after the value from the previous call
var reg=/[a-z]{3}[1-7]/ig var str="ABC3ccc9bbb6" var re=reg.test(str) console.log(re); var re2=reg.exec(str)//straight back out var re3=reg.exec(str)//The second call will start looking after the value of the previous call console.log(re2); console.log(re3);
clone regular expression
reg.source represents a string in reg
var reg2=new RegExp(reg.source)
var str="helloabc" var count=0 var temp=0 while(str.indexOf("lo",temp)!=-1){//Returns the position of the first occurrence starting at temp=0 and continuing if not equal to -1 count++ temp=str.indexOf("lo",temp)+1 } console.log(count);
Return to the first occurrence
re=str.indexOf("lo")
Determine the number of repetitions
str.indexOf("lo",temp)!=-1 indicates that the result is not equal to -1 and continues, which means that lo is matched to continue, no match means that the retrieval has been completed and return -1 while end
temp=0 means starting from the 0 subscript to detect the position where lo appears
Each loop temp is equal to the last matched position + 1
count is the number matched by the counter
var str="helloabc" var count=0 var temp=0 while(str.indexOf("lo",temp)!=-1){//Returns the position of the first occurrence starting at temp=0 and continuing if not equal to -1 count++ temp=str.indexOf("lo",temp)+1 } console.log(count);