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.

7
  • Depends on what can actually be in input. If it's just bracketed by <Code>...</Code> then it's pretty trivial. Commented Feb 1, 2021 at 19:05
  • If the tags are always at the beginning and end, you can just use slice (or substring, but slice is probably easier for finding the end index). Commented Feb 1, 2021 at 19:07
  • Yes it's just bracketed by <Code> Error in Header </Code> @DaveNewton Commented Feb 1, 2021 at 19:08
  • let input = "<Code> Error in Something </Code>"; let item1 = (input.split("<Code>")[1]).split("</Code>")[0]; item1 = stuff between two tags. Commented Feb 1, 2021 at 19:08
  • 2
    or easier: let output = "<Code> Error in Something </Code>".replace(/<\/?Code>/, '').trim(); console.log(output); Commented Feb 1, 2021 at 19:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.