1.repeat method: repeat a string n times. For example: repeat("chaojidan",2) -> chaoschaojidanchaojidan
Method 1:
The code copy is as follows:
function repeat(str,n){
return Array.prototype.join.call({length:n+1},str);
//Execute the join method in the context of the class array {length:n+1} and pass in str. That is, the option to use str to separate class arrays. The class array is empty, so there are n strs separated n+1 "", and the result is n str connections.
}
Method 2:
The code copy is as follows:
function repeat(str,n){
var s = str ,total = "";
while(n>0){
//Suppose n is 5, after n%2, it is equal to 1, so total = str.s=str. n=2. The second loop: s=strstrstrstrstrstr, n=1. The third loop total = strstrstrstrstrstr, break, jump out of the loop, return total, just the string that str has been repeated 5 times
if(n%2 ==1){
total + =s; //Here is the power of 0 of 2, that is, 1. All positive integers can be combined using 1, 2, 4, 8.... For example: 3=1+2,5=1+4,7=1+2+4.
}
if(n==1) break;
s+=s; //The power of 2 is used here, 2, 4, 8...
n = n>>1;
}
return total;
}
2. Take the length of all bytes of the string: str.charCodeAt(i) >255 and add str's length once and it's OK.
3. Convert the camel style: str.replace(/[-_][^-_]/g,function(match){return match.charAt(1).toUpperCase();})
//-_In [], / is not necessary, and ^ in [] means inverse, that is, when encountering -a or _a, it is replaced with A (match is the regular matching string _a, then take a, and capitalize)
4. Convert to underscore style: str.replace(/([az/d])([AZ])/g,'$1_$2').replace(//-/g,'_').toLowerCase();
//The first replace, matches cA, or 4A string, and replaces it with c_A or 4_A. $1 represents the first subexpression. The second replace is to use _replace-. Since - is not in [], you need to add /.
5. Remove the html tag in the string: str.replace(/<[^>]+>/g,''), the script tag will be removed, but the js script in the script will not be removed.
6. Remove the script tag and remove the js script inside: str.replace(/<script[^>]*>(/S/s)*?)<//script>/img,'')
/Required to use / to prevent escaping.
//(/S/s)*?) As few matches as possible, non-greedy matches. For example: <script>aaa</script>dddd<script>bbbb</script> will first match <script>aaa</script>, and then match <script>bbbb</script>. If there is no addition, it will be a greedy match. All <script>aaa</script> will be matched, and all the hyphen strings will be removed.
7. Escape the string through html to get the content suitable for display on the page.
str.replace(/&/g,'&').replace(/</g,'<').replace(//g,'>').replace(/"/g,'').replace(/'/g,''');
8. Change the html entity character of the string to the corresponding character:
In contrast to 7, there is only one more replace(/&#([/d]+);/g,function($0,$1){ return String.fromCharCode(parseInt($1,10)) }) //$1 is the first subexpression match.
9.trim:str.replace(/^/s+ | /s+$/g,'') , IE or early standard browsers, did not list many characters that were originally blank as /s, so there would be bugs. However, why bother to be compatible with eliminated browsers?