1. String: A read-only array of characters consisting of multiple characters
vs array: same: 1. subscript, 2. .length, 3. traverse, 4. .slice
Different: different types! API is not generic
API: All string APIs have no right to modify the original string, only return a new string
Case conversion: Convert the letters in the string to uppercase/lowercase uniformly
When: when case insensitive
How to: str.toUpperCase() to uppercase
str.toLowerCase() to lowercase
Note: SQL statements are case insensitive by default
If you want the password to be case sensitive: binary upwd=$upwd
Get the character at the specified position:
str[i] str.charAt(i)
Get the unicode number of the character at the specified position
var unicode = str.charCodeAt (i)
Convert unicode numbers back to words:
var char=String.fromCharCode(unicode)
Select substring:
str.slice(starti,endi+1) including the head but not the tail
.slice(starti,-n) to the nth last
=> .slice(starti, str.length-n)
.slice(starti) select to the end
.slice() select all
.slice(starti,starti+n) select n
str.substring(starti, endi+1) is the same as slice
Problem: Negative arguments are not supported
str.substr(starti,n) select n
Search keywords: 4 types:
1. Find where a fixed keyword appears:
var i=str.indexOf("Keyword",fromi);
In str, starting from the fromi position, find the position where the next "keyword" appears
Return value: The position of the first keyword
If not found, return -1
var i=str.lastIndexOf("Keyword");
Find the last occurrence of "keyword" in str
Problem: Keywords may change
Solution: Use regular expressions to find keywords:
2. Determine whether it contains keywords:
var i=str.search(/regex/)
Find the position of the first keyword in str that matches the regular expression rules
Return value: the position of the keyword
If not found, return -1
Problem: All regex, case-sensitive by default
Solution: add i, ignore after the second /
Problem: Can't get content for keywords
3. Get the content of the keyword:
1. Get only the content of the first keyword:
var arr=str.match(/regex/i);
Find the position and content of the first keyword in str that matches the requirements of the regular expression
Return value: [ 0: keyword, index: position]
Returns null if not found
2. Get the content of all keywords:
var arr=str.match(/regex/ig);
where g: global
Returns an array of all keywords
Problem: Can only get the content, not the position of each keyword:
4. Find the content of each keyword and find the location of each keyword:
Replacement: 2 types:
1. Simple replacement: replace all sensitive words with uniform values
str=str.replace(/regular expression/ig, "replacement value");
2. Advanced replacement: dynamically select different replacement values according to each sensitive word
str=str.replace(/regular expression/ig, function(kw){
//kw: can automatically get a keyword found this time
return According to the different kw, dynamically select different return values
});
Rationale: The callback function function() is automatically called once for each found keyword. When called, the parameter kw will automatically catch the current keyword. The new value returned by return will be replaced at the position of the keyword.
Derived operations: delete: replace with empty string
str=str.replace(/正则/ig,"")
Cut:
2. Regular expression:
What is: A rule that specifies the regularity of the occurrence of characters in a string
When: 2 types:
1. Use a set of rules to fuzzy match a variety of sensitive words
2. Use rules to verify whether the string meets the format requirements
how:
1. The simplest rule: the keyword itself
2. Character set:
what is: a list of all alternate characters on a character
When: If there are multiple alternatives on one character
How to: [alternative character list]
Emphasis: A character set that can only match one character
Shorthand: If some characters in the character set are consecutive, you can use - omit intermediate characters: For example:
1 digit: [0-9]
1 lowercase letter: [az]
1 capital letter: [AZ]
1-digit letter: [A-Za-z]
1 letter or number: [0-9A-Za-z]
1-digit Chinese character: [\u4e00-\u9fa5]
Except: [^47]
3. Predefined character sets:
\d One digit: [0-9]
\w One letter, number or underscore: [0-9A-Za-z_]
\s One space character: space, Tab, ...
. wildcard
Problem: The number of occurrences of the character set cannot be specified flexibly
4. Quantifiers:
What is: Rules for specifying the number of occurrences of a character
When: When only flexible definition of the number of times a character occurs
How to: A quantifier that follows a character set by default modifies the number of adjacent previous character sets
2 categories:
1. With a clear quantitative boundary
{n,m} at least n, at most m
{n,} at least n, more are not limited
{n} must be n
2. No clear quantitative boundaries
* Optional, more unlimited
? Optional, at most one
+ at least one, more is unlimited
5. Select and group:
1. Select: or |
What is: Between multiple sets of logic, you can choose one of them to match
When: As long as any one matches between sets of logic
2. Grouping: ( )
What is: Wrap multiple logics into one set
Problem: By default, a quantifier can only modify one adjacent character set
When: If you want a quantifier to modify multiple character sets at the same time
ID number:
15 digits 2 digits 1 digit or X
The last three are optional as a whole, at most 1 time
\d{15}(\d{2}[0-9X])?
Phone number:
+86 or 0086 optional, up to 1 time
Null characters are optional, there is no limit to more
1
3~8
9 digits
(\+86|0086)?\s*1[3-8]\d{9}
?/+
6. Specify the matching location:
^ start of string
$ end of string
When: whenever it matches the beginning or end of the content
For example: null character at the beginning of ^\s+
\s+$ trailing null character
^\s+|\s+$ leading or trailing null character
\bWord boundaries: spaces, punctuation, beginning, ending, . . .
For example: the first character of each word: \b[az]
a word no \bno\b