In JS, a regular expression is used to intercept a keyword after this keyword __HTML5

Source: Internet
Author: User

Problem Description: A section of string contains multiple keywords, the string after this keyword is intercepted together to form a key value pair
My idea is to use what to intercept, JavaScript provides three ways to intercept string neutrons:
1.substring (Str,end)
STR is required to be entered and must be positive;
End is optional and must be positive;
by literal means, Str is the starting position of the interception, and the first character position of the string is 0;end as the end position of the intercept.
The substring returned by the substring () method includes the character at start, but does not include the character at the end.
2.slice (Str,end)
The use of the slice () method is basically consistent with the substring, but the slice () parameter allows negative values;
3.substr (Str,length)
STR is required to enter, and STR is allowed to be negative, with the same usage as slice ();
Length is the lengths of the intercepted string;
I might not be able to apply these three methods well, I chose to do it with a regular expression, first using an array to access all the keywords, and then using regular expressions to intercept the string between two keywords, my code is: var reg =/keys[v][\\s|\\s]*keys[v+1]/ ;
var arr=mystr.match (REG) [0]; The string that represents the end of the keys[v] start keys[v+1]
But here are two problems, one is the need to use dynamic splicing regular expressions, and the second is to take the string can not contain the keyword KEYS[V] and keys[v+1]. Here is the improved code:

var reg = eval ('/' +keys[v]+ ' [\\s|\\s]* ' + ' (? = ' +keys[v+1]+ ')/');
 var arr=mystr.match (REG) [0];

But this still contains the keyword KEYS[V], I will use replace () to remove keys[v]

OBJ[KEYS[V]] =arr.replace (eval ('/' +keys[v]+ '/'), ');

Most of it's done, but there's one last special case, here's the complete code.

var mystr= "▇ basic information title The notification process number for the development of an overseas cultural market plan of Action (2016-2020) (2016) No. No. 0144, external communication ▇ file information to
    The text number commercial service trade sends [2016]308 the date 2016-09-14 "; 

            attribute var keys=["title", "Process number", "Serial number category", "Communication Number", "in-date"];
                    function test (keys) {var obj = new Object (); for (Var v=0 v< keys.length-1 v++) {var reg = eval ('/' +keys[v]+ ' [\\s|\\s]* ' + '] (? = ' +keys[v+1]
                         +')/');

                        var arr=mystr.match (REG) [0];

                    OBJ[KEYS[V]] =arr.replace (eval ('/' +keys[v]+ '/'), ');
                         } if (v=keys.length-1) {var reg1 = eval ('/' +keys[v]+ ' [\\s|\\s]* ' + '/');

                        var arr1=mystr.match (REG1) [0];
                        OBJ[KEYS[V]] =arr1.replace (eval ('/' +keys[v]+ '/'), ');
            return obj; } console.log (Test (keys)); 

In fact, encountered a lot of small problems in the middle, because of their JS base is too poor, need to accumulate slowly, and learned a new knowledge good happy, although the process of pain, but fortunately completed the task. Results graph:

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.