Implement string substring method of the renminbi lowercase to uppercase, digital reversal, regular optimization _javascript tips

Source: Internet
Author: User
Limited level, do not guarantee that my plan is absolutely correct, if there are errors welcome to point out.

substring method of realizing string by oneself

Method One: Use Charat to remove the interception part
Copy Code code as follows:

String.prototype.mysubstring=function (Beginindex,endindex) {
var str=this,
Newarr=[];
if (!endindex) {
Endindex=str.length;
}
for (Var i=beginindex;i<endindex;i++) {
Newarr.push (Str.charat (i));
}
Return Newarr.join ("");
}

Test
"Hello world!". Mysubstring (3);//"Lo world!"
"Hello world!". Mysubstring (3,7);//"Lo W"

method Two: Convert the string into an array and then take out the required part
Copy Code code as follows:

String.prototype.mysubstring=function (Beginindex,endindex) {
var str=this,
Strarr=str.split ("");
if (!endindex) {
Endindex=str.length;
}
Return Strarr.slice (Beginindex,endindex). Join ("");
}

Test
Console.log ("Hello world!"). Mysubstring (3));//"Lo world!"
Console.log ("Hello world!"). Mysubstring (3,7));//"Lo W"

method Three: Take out the tail part, then use Replace to remove the redundant parts, suitable for beginindex small, string length-endindex small case
Copy Code code as follows:

String.prototype.mysubstring=function (Beginindex,endindex) {
var str=this,
Beginarr=[],
Endarr=[];
if (!endindex) {
Endindex=str.length;
}
for (Var i=0;i<beginindex;i++) {
Beginarr.push (Str.charat (i));
}
for (Var i=endindex;i<str.length;i++) {
Endarr.push (Str.charat (i));
}
Return Str.replace (Beginarr.join (""), ""). Replace (Endarr.join (""), "");
}

Test
Console.log ("Hello world!"). Mysubstring (3));//"Lo world!"
Console.log ("Hello world!"). Mysubstring (3,7));//"Lo W"

less than 2 trillion or 1 trillion of the renminbi lowercase to uppercase, temporarily do not consider the continuous treatment of 0
Method One (seven lines of code implementation):
Copy Code code as follows:

function Moneycaseconvert (num) {
var upperarr=["0", "one", "II", "San", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine"],
Levelarr=["", "Pick Up", "Bai", "Qian", "Million", "Pick Up", "Bai", "Qian", "billion", "Pick Up", "Bai", "Thousand", "Million"],
Numarr=num.tostring (). Split (""). Reverse (),
Result=[];
for (Var i=numarr.length-1;i>=0;i--)
Result.push (Upperarr[numarr[i]]+levelarr[i]);
Return Result.join ("");
}

Test
Console.log (Caseconversion (1234567891234));
One million thousands of Woo Jun Lu Ying qi Bai ba pick up the Wan one thousand to pick up the restaurant

Console.log (Caseconversion (987654321));
Nine billion ba thousand Qi Bai lu pick up Wu Wu Qian restaurant three

Console.log (caseconversion (1234));
One thousand to ten

third, the digital reversal, input 123 return 321
Method One:
Copy Code code as follows:

function Numreverse (num) {
Return parseint (Num.tostring (). Split (""). Reverse (). Join (""));
}
Test
Console.log (Numreverse (123456));
654321

Method Two:
Copy Code code as follows:

function Numreverse (num) {
var numarr=num.tostring (). Split (""),
Len=numarr.length,
result=0;
for (Var i=len-1;i>=0;i--) {
Result+=numarr[i]*math.pow (10,i);
}
return result;
}

Test
Console.log (Numreverse (123456));
654321

Iv. The JSONP principle and the relationship with Ajax
JSONP principle is to use the script tag dynamic analysis JS characteristics, through dynamic add <script> tag to invoke the server provided by the JS script, to reach the target of Cross-domain call. For more information on JSONP can refer to this article: Http://www.jb51.net/article/31167.htm.

Jsonp and Ajax look a bit like, the same purpose, is to request a URL, and then the server returned the data processing, but Jsonp and Ajax is two different things, which can be said that there is no relationship between the two.

Optimization of regular expressions

The optimization of regular expressions is generally as accurate as possible, reducing the number of backtracking, specifically in the following points:

1. If your regular tool is supported, use non-capture brackets when you do not need to refer to the bracketed text: (?: expression).
2. If the brackets are not necessary, do not add parentheses.
3. Do not misuse character arrays, such as [.], please use \. 。
4. Use Anchor point ^ $, which accelerates positioning.
5. Extract the necessary elements from two times, such as: x+ written xx*,a{2,4} written aa{0,2}.
6. Extract the same characters at the beginning of the multiple-selection structure, such as the|this to th (?: E|is). (If your regular engine does not support this use, change to th (e|is)), especially the anchor point, must be independent, so many regular compilers will be based on the anchor point for special optimization: ^123|^ABC change to ^ (?: 123|ABC). The same $ is also as independent as possible.
7. An expression behind the multiple-selection structure is placed in a multiple-selection structure, which allows the match to fail faster when matching any one of the multiple-selection structures without exiting the multiple-selection structure. This optimization needs to be used with caution.
8. Ignoring priority matching and priority matching needs to be determined by your situation. If you're unsure, use a match priority, and it's faster than ignoring priority.
9. Splitting large regular expressions into small regular expressions is very helpful to improve efficiency.
10. Simulate the anchor point, use the suitable survey structure to predict the suitable starting match position, if match 12 months, can check first the first character to match firstly: (? =jfmasond) (?: jan| feb|...| DEC). This optimization is used according to the actual situation, and sometimes the overhead of looking around the structure may be greater.
Note: The above excerpts from http://www.jb51.net/article/31168.htm, very good a regular expression optimization article, recommend interested friends to see the original.

vi. The difference between Visibility:hidden and Display:none
The difference is mainly three points:

1, space occupy: The element is set to Visibility:hidden after still occupy the physical space, and the element is set to Display:none is not occupy space.

2, Performance: Visibility:hidden because still occupy the physical space, so will not cause the reflow and redraw the page, so the performance is better, conversely display:none will cause the page reflow and redraw.

3, Inheritance: When the parent element is set to display:none all child elements will be hidden, do not change the visibility of the parent element is not a way to display the element, if the parent element is set to Visibility:hidden, all child elements will not be visible, But if you set the child element to visibility:visible, the child element can be brought to the daylight.

Seven, the calendar DOM tree

Method One: Use NextSibling and ChildNodes
Copy Code code as follows:

function traversalbynextsibling (obj) {
var ch=obj.firstchild,
Result=[];
do{
Result.push (Ch.nodename);
if (ch.childNodes.length) {
Result.push.apply (result,traversalbynextsibling (ch));
}
}while (ch=ch.nextsibling);
return result;
}

Method Two: Use ChildNodes
Copy Code code as follows:

function Traversalbychildnodes (obj) {
var ch=obj.childnodes,
Result=[];
for (Var i=0,j=ch.length;i<j;i++) {
Result.push (Ch[i].nodename);
if (ch[i].childnodes.length) {
Result.push.apply (Result,traversalbychildnodes (ch[i));
}
}
return result;
}

Test:
Copy Code code as follows:

<! DOCTYPE html>


<meta charset= "Utf-8" >
<title>Demo</title>
<style type= "Text/css" >

</style>

<body>
<div id= "Test" >Test</div>
<div>hello world</div>
<p>PTest</p>
<script>
Console.log (traversalbynextsibling (document));
Ie6-8: #comment, Html,head,title,meta,style,body,div, #text, DIV, #text, P, #text, SCRIPT
other:["HTML", "HTML", "Head", "#text", "META", "#text", "TITLE", "#text", "#text", "STYLE", "#text", "#text", "#text", "Body", "#text", "div", "#text", "#text", "DIV", "#text", "#text", "P", "#text", "#text", "SCRIPT", "#text"]

Console.log (Traversalbychildnodes (document));
Ie6-8: #comment, Html,head,title,meta,style,body,div, #text, DIV, #text, P, #text, SCRIPT
otehr:["HTML", "HTML", "Head", "#text", "META", "#text", "TITLE", "#text", "#text", "STYLE", "#text", "#text", "#text", "Body", "#text", "div", "#text", "#text", "DIV", "#text", "#text", "P", "#text", "#text", "SCRIPT", "#text"]
</script>
</body>


In the ie6-8 to remove the line, in other browsers as a text node, so there will be a lot of #text, but Ie6-8 appeared in #comment I do not understand why.

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.