Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

you are overkilling the solution, you can do it easily with the function replaceAll which will replace the occurrences of your search, in this case you will need to use some regex in order to do a proper replacement, so you can use &(\w+)& where the (\w+) part will capture the group between the & and then you can use it in the replace call as $1.

here is your code with the implementation of the explanation above

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

const replaceAmpersandsWithHashtag = (text) => {
    return text.replaceAll(/&(\w+)&/g, '#$1');
}

const replaceHashtagWithAmpersands = (text) => {
    return text.replaceAll(/#(\w+)/g, '&$1&');
}

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!