Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
preface
Based on learning
Start with a small chestnut:
let req = '/at/gi'; // Matches all ats in the string case insensitive
Copy the code
That’s what emmmm notes say, but what are we to make of it?
let req = '/pattern/flags'
Copy the code
Pattern can be any simple or complex regular expression, including character classes, qualifiers, grouping, look-forward, and backreferencing
Flags, which control the behavior of the re, are covered directly
- G: Global mode, which looks for the entire content of the string instead of finding the first match
- I: Case insensitive, indicating that the case of pattern and string are ignored when searching for a match
- M: Multi-line mode: the search continues after the end of a line of text
- Y: in adhesive mode, the search continues after only the end of a line of text
- U: Unicode mode, enabling Unicode matching
- S: dotAll pattern, representing metacharacters, matching any character (including \n or \r)
Rich text intercepts internal content
var a = document.createElement("div") // Create a container to store rich text content
a.innerHTML = '<div>111<span>22222</span></div>' // Add elements to the container
a.innerText / / "11122222"
Copy the code
With or without decimals
function isDecimal(strValue) {
var objRegExp= /^\d+\.\d+$/;
return objRegExp.test(strValue);
}
Copy the code
Verify whether the check is composed of Chinese names
function ischina(str) {
var reg=/ ^ [\ u4E00 - \ u9FA5] {2, 4} $/; /* Define the validation expression */
return reg.test(str); /* Verify */
}
Copy the code
Check whether the check is made up of 8 digits
function isStudentNo(str) {
var reg=/ ^ [0-9] {8} $/; /* Define the validation expression */
return reg.test(str); /* Verify */
}
Copy the code
Verify the phone code format
function isTelCode(str) {
var reg= / ^ ((0 \ d {2, 3} - \ d {7, 8}) | (1 [3584] \ d {9})) $/;
return reg.test(str);
}
Copy the code
Verify that the email address is valid
function IsEmail(str) {
var reg=/ ^ + @ \ w/a - zA - Z0-9] {2, 10} (? : \ [a-z] {2, 4}) $/ {1, 3};
return reg.test(str);
}
Copy the code
Replace specific files to preserve variables in the middle
The current demand is
< P > Small scraper refers to the bucket volume in 3m³ The following LDS. < / p > requirements:
-
Replace & sup3; < sup > 3 < / sup >
-
Keep & sup3; Constant in the middle
let str = '< P > Small scraper refers to the bucket volume of 3m³ The following LDS. '
str.replace(/&sup(.*?) ; /.'<sup>$1</sup>')
Copy the code
Id number part shows hidden
IDcardNo() {
const card = '123845785238542307'
if (this.showIdCardNo) {
console.log(1111)
return card
}
// Intercepts the string from the third to last digits of the id number
const reg = card.slice(3, -3)
// Add * according to the truncated length
const pad = The '*'.padStart(reg.length, The '*')
return card.replace(reg, pad)
}
Copy the code
String interception
analysisUrl(url: string): any {
if (url) {
// String interception
url = url.substr(url.indexOf('? ') + 1)}// Create an object for name and value
const result = {},
queryString = url || location.search.substring(1), //location.search sets or returns from question mark (?) The starting URL (query section).
re = /([^&=]+)=([^&]*)/g
let m: any = ' '
while ((m = re.exec(queryString))) {
//exec() matches regular expressions
// Use decodeURIComponent() to decode the encoded URI
result[decodeURIComponent(m[1=]]decodeURIComponent(m[2])}return result
}
Copy the code
conclusion
Here are some tips for you! If you like it, please collect it. If you don’t like it, please make fun!