Problem with my regex for the american-british translator

Hello all,

I have a problem in the american-british translator. For the moment, i have implemented the routes of the application and I want to implement the translator itself.

Let’s focus on the translation of words. First, I implemented something that worked, but I encountered some issues. For example, “paracetamol” from british to american was translated “paraceThank youmol”, because “ta” has a translation from british to american.

So I tried to make a regex which match the word if it is at the beginning or the end of the tring or if it is surrounded by spaces.
This is the regex : /(?<=\s|^)(word)(?=\s|$)/gi
I tested it on https://regex101.com/ and it has the expected behaviour.

So here is the code I made to translate a sentence and let’s test it for the string “I try to acclimate myself to this cold weather”

  translate(text, mode){
    let newString = text;
    if(mode === "american-to-british"){
      //transationList is an array of arrays. Each array contains the word in american and the corresponding word in british
      translationList.forEach(translation => {
        if(translation[0]==="acclimate"){
        //I put this so only one console log is shown
        let regWord = new RegExp("(?<=\s|^)("+translation[0]+")(?=\s|$)", "gi");
        console.log(translation[0]);   //shows "acclimate" as expected
        console.log(translation[1]);  //shows "acclimatise" as expected
        console.log(regWord); //shows the regex as expected
        console.log(newString.match(regWord)); //shows null
        newString = newString.replace(regWord, translation[1]);
        
        }
      });
    }
}

What do I do wrong ?

My repl.it : https://repl.it/talk/share/FCC-American-British-translator/121205
The link to the project : https://www.freecodecamp.org/learn/quality-assurance/quality-assurance-projects/american-british-translator

PS : I know i should return the word in green and in the same case, but it is a problem for later !

Why do you need regex at all?
Can’t you just go through the words in the sentence and check if relevant dictionary has a key?
Something like this:

function translate(sentence, dictionary) {
  return sentence
    .split(/\b/)
    .map((word) => dictionary[word] || word)
    .join('');
}

const amToBrDict = { color: 'colour' };

translate('It is a nice color.', amToBrDict);

P.S. You didn’t post the link to the challenge and I’m to lazy to look it up, so I’m not sure what are the exact requirements.

I need a regex because the word to translate can actually be 2 words.

The link of the project is this : https://www.freecodecamp.org/learn/quality-assurance/quality-assurance-projects/american-british-translator

I add it in the original post

I’m still not convinced you need regex :slight_smile:
Another attempt:

function translate(sentence, dictionary) {
  let result = sentence;
  Object.keys(dictionary).forEach((key) => {
    const index = result.indexOf(key);
    if (index > 0) {
      result = result.replace(key, dictionary[key]);
    }
  });

  if (result === sentence) {
    return 'Everything looks good to me!';
  }

  return result;
}

const amToBrDict = { 'sedan automobile': 'saloon', color: 'colour' };

const x = translate('This sedan automobile has nice color.', amToBrDict);
console.log(x);

Hmm I see two problems :
If i put “paracetamol” at the beginning of the string, it becomes “paracethank youmol” again but if i put it at the middle or at the end, it works properly.

Also, it translates the word if it is inside another word, so if I write “paracetamoleee”, it is translated as “Typhenoleee”.

So that’s why i wanted to try using a regex to be sure the word was actually a word and not a part of another word.

Thank you for taking time to help me by the way !

So i reorganized the translationList and replace my regex by this one :

/(?<![\\w-])word(?![\\w-])/gi 

It seems to work properly. I still have to figure out why the previous one didn’t have the expected behaviour.

I was having trouble with ‘trashcan’ translating to ‘rubbishcan’ instead of bin as a whole word.

This is my way of working around it.

        let search = '\\b'+ key + '\\b'
        let re = new RegExp(search, 'gi')
        translation = translation.replace(re, americanOnly[key])

Hope you find your way around yours