Home > Web Front-end > JS Tutorial > Expansion and repair of strings in javascript framework design for reading notes_javascript skills

Expansion and repair of strings in javascript framework design for reading notes_javascript skills

WBOY
Release: 2016-05-16 16:29:39
Original
1415 people have browsed it

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

Method 1:

Copy code The code is as follows:

function repeat(str,n){
return Array.prototype.join.call({length:n 1},str);
//Execute the join method in the context of class array {length:n 1} and pass in str. That is, use str to separate the options of the class array. The class array is empty, so there are n str separated by n 1 "", and the result is n str connections.
}

Method 2:

Copy code The code is as follows:

function repeat(str,n){
var s = str ,total = "";
​while(n>0){                                                               //Assume n is 5, after n%2, it is equal to 1, so total = str.s=strstr. n=2. The second loop: s=strstrstrstr, n=1. The third loop total = strstrstrstrstr, break, jump out of the loop and return total, which happens to be the string where str is repeated 5 times
  if(n%2 ==1){   
   Total =s; //Here is 2 raised to the 0th power, which 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; //What is used here is the power of 2, 2, 4, 8....
  n = n>>1;
}
return total ;
}

2. Get the length of all bytes of the string: str.charCodeAt(i) >255. Just add the length of str once and it will be OK.

3. Convert camel case style: str.replace(/[-_][^-_]/g,function(match){return match.charAt(1).toUpperCase();})

//-_In [], there is no need to use it, and ^ in [] means the opposite, that is, when -a or _a is encountered, it will be replaced by A (match is a regular matching string _a, then take a and capitalize it)

4. Convert to underline style: str.replace(/([a-zd])([A-Z])/g,'$1_$2').replace(/-/g,'_').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 replacement is to use _ to replace -. Since - is not in [], it needs to be added.

5. Remove the html tag in the string: str.replace(/<[^>] >/g,''), which will remove the script tag, but will not remove the js script in the script.

6. Remove the script tag and remove the js script inside: str.replace(/]*>(Ss)*?)/img,'')

/ Need to be used to prevent escaping.

//(Ss)*?) Match as little as possible, non-greedy matching. For example: <script>aaa</script>dddd<script>bbbb</script> will match <script>aaa</script> first, then <script>bbbb</script>, if not Adding ? will be a greedy match, and will match all <script>aaa</script>dddd<script>bbbb</script>, even the string dddd will be removed.

7. Escape the string through html to obtain content suitable for display on the page.

str.replace(/&/g,'&').replace(//g,'>').replace(/"/ g,'"').replace(/'/g,''');

8. Replace the html entity characters of the string with corresponding characters:

The opposite of 7, just 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 do not list many blank characters as s, so there will be bugs. However, why insist on being compatible with obsolete browsers?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template