javascript - How to split this variable?
天蓬老师
天蓬老师 2017-06-06 09:54:11
0
5
697

quote_ABC_123123='jsonparse1';

quote_ABC_123123123='jsonparse2';

Regularly obtains the characters after the second _ and the following string to form an array

[123123:jsonparse1,123123123:jsonparse2]

How to implement it with js? = =

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(5)
洪涛
var obj = {};

["quote_ABC_123123='jsonparse1'", "quote_ABC_123123123='jsonparse2'"].forEach(v => {
    v.replace(/.*_(\d*)='(.*)'/, (a, b, c) => {
        obj[b] = c;
    });
})

console.log(obj);
世界只因有你

If the composition format is fixed, it can be like this:
str.split("_")[2];

or lastIndexOf gets the last "_" position and intercepts the following string,

For sure, I’m not very familiar with it, so I just use it and look it up. I’ll add more later downstairs

某草草

In php, the expression "/(1+)='(w+)';$/" can extract the characters on both sides of the equal sign


  1. _ ↩
巴扎黑
  1. Regular php:

        preg_match("/quote_ABC_(.+?)='(.+?)'/", "quote_ABC_123123='jsonparse1'", $matches);
        var_dump(array($matches[1] => $matches[2]));
        
  2. explode php:

        $arr    = explode('_', "quote_ABC_123123='jsonparse1'");
        $result = explode('=', $arr[2]);
        var_dump(array($result[0] => trim($result[1], "'")));
淡淡烟草味

The answer is already there, I will add it. If the prefix is ​​quote_ABC, it is best to write quote_ABC in the third digit to confirm whether the requirement is just a number
and whether there will be double quotes in the following single quotes. In addition, forEach is an ES5 API

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template