search

Home  >  Q&A  >  body text

javascript - How to split this variable?

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? = =

天蓬老师天蓬老师2757 days ago673

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-06-06 09:56:11

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

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-06 09:56:11

    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

    reply
    0
  • 某草草

    某草草2017-06-06 09:56:11

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


    1. _ ↩

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-06 09:56:11

    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], "'")));

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-06 09:56:11

    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

    reply
    0
  • Cancelreply