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

Function that replaces the wrapped word with ampersands with just single hashtag at start

I tried to write a function to replace ampersands which are arround word in string with just single hashtag which will be at the beginning of word &word& => #word. And I did it, but after looking at my code I can tell that it’s kind of ugly, and also I need function that will return text to version with ampersands too. So, maybe someone can tell me how can I improve it? Maybe someone can provide version with regex?

const text = "&string& someText&string2& &string3& &string4&&string5&";

const replaceAmpersandsWithHashtag = (text: string) => {
    const firstStep = text.replace(/\&/g, '#');
    let occurenceOfHashtag = 0;
    const secondStep = firstStep.split('').filter(char => {
      if(char === '#') {
        occurenceOfHashtag += 1 ;
        return occurenceOfHashtag % 2 === 0 ? false : true;
      }
      return true
      }).join('');
    return secondStep
}

const replaceHashtagWithAmpersands = (text: string) => {
    const firstStep = text.replace(/\#/g, '&');
    let isLookingForNextAmpersand = false;
    const secondStep = firstStep.split('').map((char, index) => {
      if(char === '&') {
        isLookingForNextAmpersand = true;
        return char;
      }
      if(isLookingForNextAmpersand && firstStep.length === index + 1) {
        isLookingForNextAmpersand = false;
        return `${char}&`
      }
      if(isLookingForNextAmpersand && (firstStep.charAt(index + 1) === ' ' || firstStep.charAt(index + 1) === '&')) {
        isLookingForNextAmpersand = false;
        return `${char}&`
      }
      return char
      }).join('');
    return secondStep
}

const textWithHastags = replaceAmpersandsWithHashtag(text);
const againWithAmpersands = replaceHashtagWithAmpersands(textWithHastags);

// Should be "#string someText#string2 #string3 #string4#string5"
console.log(textWithHastags)
// Should be "&string& someText&string2& &string3& &string4&&string5&"
console.log(againWithAmpersands)
// Should be "true"
console.log(text === againWithAmpersands)

>Solution :

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

Use a capture group in the regexp to copy the word between the & to the replacement.

function replaceAmpersandsWithHashtag(string) {
  return string.replace(/&(\w+)&/g, '#$1');
}

function replaceHashtagWithAmpersands(string) {
  return string.replace(/#(\w+)/g, '&$1&');
}

const text = "&string& someText&string2& &string3& &string4&&string5&";

const textWithHastags = replaceAmpersandsWithHashtag(text);
const againWithAmpersands = replaceHashtagWithAmpersands(textWithHastags);

// Should be "#string someText#string2 #string3 #string4#string5"
console.log(textWithHastags)
// Should be "&string& someText&string2& &string3& &string4&&string5&"
console.log(againWithAmpersands)
// Should be "true"
console.log(text === againWithAmpersands)
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