Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript: parse a string and split it between tags and text

I am building a script that takes a string and separates left and right part based on some characthers.

However, I am having some difficulties handling edge cases.

The script will be of the following format: @tag1@tag2@tag3:lorem ipsum quare id... and I would like to get to get something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
 tags:["tag1", "tag2", "tag3"],
 text:"lorem ipsum quare id..."
}

However, there are some edge cases that I need to consider:

  • The number of initial "@tag" could vary from 0 to n
  • If there are 0 "@tag", there won’t be any ":" to separate the tag part from the "text" and we should return a default tag "default"

Examples

  • @mario:lorem ipsum should return {tags:['mario'], text:"llorem ipsum"}
  • @mario@luigi:lorem ipsum should return {tags:['mario','luigi'], text:"lorem ipsum"}
  • lorem ipsum should return {tags:['default'], text:"lorem ipsum"}

I initially thought about using something like this, but it surely doesn’t take into consideration these edge cases and it doesn’t work very cleanly

function splitTagText(text){
  return text.split(/:(.*)/s)
}

function extractTags(text){
  return text.split('@').slice(1)
}

function processInput(text){
  const tagText = splitTagText(text)
  const tags = extractTags(tagText[0])
  const todo = tagText[1]

  return {tags,todo}
}


const tmp1 = '@mario:lorem ipsum'
const tmp2 = '@mario@luigi:lorem ipsum'
const tmp3 = 'lorem ipsum'


console.log(processInput(tmp1)) 
console.log(processInput(tmp2)) 
console.log(processInput(tmp3)) // breaks



>Solution :

/^(@.+?:)?(.+)/ appears to work fine:

const tmp1 = '@mario:lorem ipsum'
const tmp2 = '@mario@luigi:lorem ipsum'
const tmp3 = 'lorem ipsum'


function processInput(s) {
    let m = s.match(/^(@.+?:)?(.+)/)

    return {
        tags: m[1] ? m[1].slice(1, -1).split('@') : ['default'],
        text: m[2]
    }
}



console.log(processInput(tmp1))
console.log(processInput(tmp2))
console.log(processInput(tmp3))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading