0

Use case:

I have a function that receives a string and I want to parse the <Code> .... </Code> and get the contents in between the code block.

const input = `<Code> Error in Something </Code>`
const getErrorMessage = parseCode(input); // Error in something

I'm confused on how to proceed. I initially thought of proceeding with RegEx but wasn't sure if there is a better solution.

Filburt
  • 17,626
  • 12
  • 64
  • 115
TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • Depends on what can *actually* be in `input`. If it's *just* bracketed by `...` then it's pretty trivial. – Dave Newton Feb 01 '21 at 19:05
  • If the tags are always at the beginning and end, you can just use [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) (or `substring`, but `slice` is probably easier for finding the end index). – John Montgomery Feb 01 '21 at 19:07
  • Yes it's just bracketed by ` Error in Header ` @DaveNewton – TechnoCorner Feb 01 '21 at 19:08
  • let input = " Error in Something "; let item1 = (input.split("")[1]).split("")[0]; item1 = stuff between two tags. – raddevus Feb 01 '21 at 19:08
  • 2
    or easier: `let output = " Error in Something ".replace(/<\/?Code>/, '').trim(); console.log(output);` – Randy Casburn Feb 01 '21 at 19:09
  • @RandyCasburn but this is producing this output `Error in Something ` so the code close tag is still present. I am new to this and would appreciate your response. – TechnoCorner Feb 01 '21 at 19:15
  • Of course - because I forgot he greedy flag: `/<\/?Code>/g` sorry about that – Randy Casburn Feb 01 '21 at 19:20

0 Answers0