Home >Web Front-end >JS Tutorial >JavaScript a question on string decomposition_javascript skills
I went to a certain company for an interview (I won’t mention the company name, but this set of questions may still be used). I did a set of questions in 30 minutes on site, and one of them was like this:
Required to write a function in js , for the incoming URL string in the following form, return the corresponding object.
For example:
If the string a='?name=zhiyelee&blog=www.tsnrose.com';
is returned, b={'name':'zhiyelee','blog':'www .tsnrose.com'}
Due to the relatively short time at that time, there were some problems with the implementation. After I came back, I thought about it and summarized it as follows:
I thought of two ideas, one is to use regular expressions Expression, the second is to use the split function of string.
1. Use regular expression processing
The first thing I thought of was to use regular expression processing. It may be that this is more challenging and the simplest to write. However, I doubted that the efficiency of this method would be It is less efficient than directly using string functions. We will verify this efficiency below~
The idea of this method is very simple, which is to use regular expressions to match a string of '***=###' each time, and then loop and finally remove them all.
The code is as follows
View the complete source code: jsfiddle, you can test it by yourself~
ps: