Structural expression in the required species



  • Hello, everyone! There's a strong expression like: [exmp_1]+[examp_2]+[examp_3]I need to evaporate the value in the brackets and from these values to create the following mass: ["+",["get","exmp_1"],["get","exmp_2"],["get","exmp_3"]]

    I need to understand what the phrase is with the deposit, that is, if, for example, the line. [exmp_1]+[examp_2][examp_3]it's not correct and it doesn't have to be an expression. They can be all they want.

    Here's my option, what he's done, but I don't know how to turn "+" and his absence. Maybe you need some other regular.

    function parcingString(text) {
      let expression;
      const parts = [];
      const regex = /[(.+?)]/g;
      const matches = [...text.matchAll(regex)];
      if (matches) {
        for (const match of matches) {
          parts.push(['get', match[1]]);
        }
      }
    

    expression = ['+'];
    parts.forEach((item) => {
    expression.push(item);
    });

    console.log('expression', expression)
    }

    let test = '[exmp_1]+[examp_2] + [examp_3]'
    parcingString(test);

    Maybe someone has ideas how to do this?
    Thank you for your attention and your answer!



  • I'd like to make sure that the line is solid, and after that, it's going through your matcher's valid mass.

    The solution can be improved if it's not to check the wheels of the line, but the validity, I guess, but I can't really do RegExp myself and help what I could.

    // Если тестить isInvalid на не валидной строке - вернётся true
    const isInvalid = new RegExp(/(?<=])[^\+]/);
    const matcher = new RegExp(/(?<=[).*?(?=])/g);
    

    function parseString (string) {
    return Array.from(string.matchAll(matcher)).reduce(function (acc, item) {
    acc.push(["get", item[0]]);
    return acc;
    }, ["+"]);
    }

    // Если передать не валидную строку, то getResult вернёт false
    function getResult (string) {
    return !isInvalid.test(string) && parseString(string);
    }

    console.log(getResult("[exmp_1]+[examp_2]+[examp_3]"));



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2