JavaScript Framework design the extension and repair of the string of reading notes _javascript tips

Source: Internet
Author: User

1.repeat method: Repeats a string to itself n times. For example: Repeat ("Chaojidan", 2)-> Chaojidanchaojidan

Method 1:

Copy Code code as follows:

function repeat (str,n) {
Return Array.prototype.join.call ({length:n+1},str);
Executes the join method under the class array {length:n+1} context and passes in Str. That is, the option of separating the class array with STR, the class array is empty, so there is n str delimited n+1 "", and the result is n str connections.
}

Method 2:

Copy Code code as follows:

function repeat (str,n) {
var s = str, total = "";
while (n>0) {
Assuming n is 5,n%2, equals 1, so total = Str.s=strstr. n=2. Second cycle: S=strstrstrstr,n=1. The third cycle total = Strstrstrstrstr,break, jump out of the loop, return to total, just the string that str repeated 5 times
if (n%2 ==1) {
Total + =s; Here is 2 of the 0 times, that is, 1, all positive integers, can use 1,2,4,8 ... to combine. 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 just add the length of the Str to the OK.

3. Switch Hump style: Str.replace (match) {return Match.charat (1). toUpperCase (/[-_][^-_]/g,function)

-_ in [], do not need to use \, and ^ in [] is to represent the opposite meaning, is to encounter a-a or _a, is replaced with a (match is a regular match of the string _a, and then take a, and uppercase)

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

The first replace, match the CA, or 4 a string, and replace it with c_a or 4_a. Represents the first child expression. The second replace is to use _ substitution-. Because-not in [], you need to add \.

5. Remove the HTML tags in the string: Str.replace (/<[^>]+>/g, ""), which will remove the script tag, but will not remove the script JS scripts

6. Remove the script tag and remove the JS script inside: str.replace (/<script[^>]*> (\s\s) *?) <\/script>/img, "")

/need to use \ to prevent escaping.

(\s\s) *?) Match as few as possible, not greedy. For example: <script>aaa</script>dddd<script>bbbb</script>, the first match <script>aaa</script , and then match <script>bbbb</script>, if not add, will be greedy match, will put <script>aaa</script>dddd<script> Bbbb</script> all match out, even the string dddd are removed.

7. Pass the string HTML escape to get the content that is suitable for display in the page.

Str.replace (/&/g, ' & '). Replace (/</g, ' < '). Replace (/>/g, ' > '). Replace (/"/g, '"). Replace (/'/g, ' '');

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

Instead of 7, just one more replace (/&# ([\d]+);/g,function ($0,$1) {return String.fromCharCode (parseint ($1,10))})//$ 1 is the first subexpression that matches the expression.

9.trim:str.replace (/^\s+ | \s+$/g, "), ie, or an earlier standard browser, puts many characters that are not part of the space in the \s, so there are bugs. However, why persist in compatible with the elimination of the browser.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.