JavaScript a string decomposition topic _javascript Tips

Source: Internet
Author: User
Go to a company (company name does not say, people this set of questions may still need to use) interview, the scene 30 minutes to do a set of questions, one of them is this:

Require JS to write a function, the incoming form of the following URL string, return the corresponding object.
Such as:
If incoming string a= ' name=zhiyelee&blog=www.tsnrose.com ';
Return b={' name ': ' Zhiyelee ', ' blog ': ' www.tsnrose.com '}


Because time is relatively short, realize some problems, came back to think about it, summed up as follows:
I think of two ways to use regular expressions, and the second is to use the split function of strings.

1. Use regular expressions to handle
The first thing I thought of was using regular expression processing, which might be more challenging to write and the simplest, but at the time it was suspected that the method was less efficient than using string functions directly. This efficiency we will verify in the following
The idea of this method is simply to use regular expressions to match a ' ***=### ' string each time, and then loop the last one out.
The code is as follows
Copy Code code as follows:

var getnrreg = function (str) {
var res = {};
var reg =/(\w+) = (\w+)/g;
while ((A = reg.exec (str))) {
RES[A[1]] = a[2];
}
return res;
};

2. Use string function to handle
This idea of thinking is also more conventional, but it is certainly better than using regular expressions to deal with some of the trouble.
My idea first uses ' & ' to split the original string into multiple strings, each string's style like ' ***=### ', and then apply split (' = ') to the string. (This method does not use regular, of course we can use regular split (/&|=/)
The code is as follows
Copy Code code as follows:

var getnrsplit = function (str) {
var temp, res = {},
I, ret;
str = str.slice (1);
temp = Str.split (' & ');
for (i = 0; i < temp.length; i++) {
ret = temp[i].split (' = ');
Res[ret[0]] = ret[1];
}
return res;
}

The efficiency of these two approaches
It is well known that the efficiency of regular expressions is a little low, so it was originally thought that the efficiency of the first scheme would be lower than that of the second scheme, and the results would not be:

Perform 1 million time consuming situations:
Getnrreg Execution Time consuming 4399ms
Getnrsplit Execution Time consuming 6116ms

Complete source Code view: Jsfiddle, you can test yourself ~

Ps:
Finally attached to the Sina Weibo 2011-06-15 Bupt in the front of the post of a written question:

A regular expression completes the deletion of a character that is adjacent to a character, such as a "FDAFFDAAKLFJK" string, and becomes "FDAFDAKLJK" after processing.

My answer is jsfiddle.
Related Article

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.