Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
Parsing string with special character and returning the output

I know its a pretty simple question, but to anyone who is new to Javascript this can be interesting.

Is there any fastest way to parse this string and get the color for the fruit like shown below :

var fruitAndColors = "APPLE=RED&GUAVA=GREEN&STRAWBERRY=RED&BANANA=yellow&ORANGE=orange"
var applecolor = getColor("APPLE") // RED
var bananaColor = getColor("BANANA") //yellow
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Here is a regex match approach:

function getColor(fruitAndColors, fruit) {
    return fruitAndColors.match(new RegExp("\\b" + fruit + "=([^&]+)"))[1];
}

var fruitAndColors = "APPLE=RED&GUAVA=GREEN&STRAWBERRY=RED&BANANA=yellow&ORANGE=orange"
console.log(getColor(fruitAndColors, "APPLE"));  // RED
console.log(getColor(fruitAndColors, "BANANA")); //yellow

For the case of searching for APPLE we use the following regex pattern:

\bAPPLE=([^&]+)

This places the key (the color) in the first capture group, which the helper function then returns.

about 4 years ago · Juan Pablo Isaza Relatório

0

Regex will probably be the best bet for this however it is definitely not my strong-suit. That said, here's what I came up with just some Javascript:

var fruitAndColors =
  'APPLE=RED&GUAVA=GREEN&STRAWBERRY=RED&BANANA=yellow&ORANGE=orange';

const getColor = (key) => {
  const entries = fruitAndColors.split('&').reduce((acc, val) => {
    const [fruit, color] = val.split('=');
    acc[fruit] = color;
    return acc;
  }, {});

  return entries[key];
};

var appleColor = getColor('APPLE'); // RED
var bananaColor = getColor('BANANA'); //yellow
about 4 years ago · Juan Pablo Isaza Relatório

0

A little class that handle that kind of url parse

class UrlParse {
    #objects;
  
  constructor(uri) {
    this.uri = uri
    this.objects = {}
    this.parse()
  }
  
  parse() {
    let arr = this.uri.split('&')
    
    for (let a of arr) {
        let arr = a.split(/\=/)
        this.objects[arr[0]] = arr[1]
    }
  }
  
  getName(name) {
    return this.objects[name]
  }
}
var fruitAndColors = "APPLE=RED&GUAVA=GREEN&STRAWBERRY=RED&BANANA=yellow&ORANGE=orange"

let p = new UrlParse(fruitAndColors)
console.log(p.getName('BANANA'))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda