The javascript framework is designed to extend and repair the strings of reading notes

  • 2020-03-30 04:27:37
  • OfStack

1. Repeat method: a string repeats itself n times. For example: repeat (" chaojidan ", 2)     - > chaojidanchaojidan

Method 1:


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 STR. That's the option to separate the array of classes with STR, which is empty, so you have n STRS separating n+1 "", and you get n STR connections. < br / > }

Method 2:


function repeat(str,n){
  var s = str ,total = "";
  while(n>0){             
//Suppose n is 5, n%2 is equal to 1, so total = STR. S = STRSTR. N =2. Second loop: s= STRSTRSTRSTR, n=1     if(n%2 ==1){    
      total + =s;     //This is 2 to the 0, which is 1, and all the positive integers, you can use 1,2,4,8... Let's combine. For example: 3=1+2,5=1+4,7=1+2+4.
    }
    if(n==1)  break;
    s+=s;    //It's 2 to the power, 2,4,8... < br / >     n = n>>1;
  }
  return total ;
}

2. Take the length of all bytes of the string: STR. CharCodeAt (I) > 255 just add the length of STR once and you're done.

STR. Replace (/[-_][^-_]/g,function(match){return match.charat (1). ToUpperCase (); })  

//-_ in [], you don't need to use \, and ^ in [] is the inverse meaning, that is, when a or _a is encountered, it is replaced with a (match is the string _a that is a regular match, and then takes a and capitalizes it).

4. Convert the underline style: STR. Replace (/ ([a-z \] d)/g ([a-z]), '$1 $2 _'). The replace (/ \ - / g, '_'). The toLowerCase ();

// the first replace, matches the string of cA or 4A, and then replaces it with c_A or 4_A. $1 represents the first subexpression. The second replace is to use _ replace -. Because - is not in [], you need to add \.

5. Remove the HTML tag from the string: STR. Replace (/< [^ >] + > /g, "), this will remove the script tag, but not the js script in the script

6. Remove the script tag and remove the js script: STR. Replace (/< The script / ^ > * > [\ S \ S) * & # 63;) < \ / script> / img, ' ')          

  / need to use \ to prevent escape.

/ / (\ S \ S) * & # 63;) As few matches as possible, not greedy matches. For example: < Script> Aaa< / script> Dddd< Script> Bbbb< / script> Will match first < Script> Aaa< / script> , match again < Script> Bbbb< / script> If you don't add ? Will be the greedy match will be the < Script> Aaa< / script> Dddd< Script> Bbbb< / script> It all matches, even the string DDDD.

7. Escape the string through HTML to get the content suitable for display on the page.

STR. Replace (/ & / g, '& amp; '). The replace (/ < / g, '< '). The replace (/ > / g, '> '). The replace ("/g, '& quot; '). The replace (/ '/ g,' & # 39; ');

8. Replace the HTML entity character of the string with the corresponding character:

Replace (/&#([\d]+); / g, function ($0, $1) {return String. FromCharCode (parseInt ($1, 10))   })     //$1 is the first subexpression match.

9. The trim: STR. Replace (/ ^ \ s + | \ s + $/ g, ' ')     , IE, or early standard browsers, had bugs because they didn't list many characters that were supposed to be white space in \s. However, why persist in compatibility with obsolete browsers.


Related articles: