Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Javascript Regex to convert from first to second person using multiple words in a dictionary

I am writing a Javascript function to convert a sentence from first person to second person. My current test function is as follows:

function statementCreator() {
    var sentence = "I went to the movies with my friend, Sally. Sally says that I'm her best friend.";

    var transforms = {
        "I" : "YOU", 
        "ME" : "YOU", 
        "MY" : "YOUR", 
        "AM" : "ARE", 
        "MINE" : "YOURS",
        "I'M" : "YOU'RE"
    };

    var pattern = `\\b(?:${Object.keys(transforms).join('|')})\\b`;
    var re = new RegExp(pattern, "g");

    str = sentence.toUpperCase().replace(re, matched => transforms[matched]);
    console.log(str);
}

This creates the following regex pattern:

/\b(?:I|ME|MY|AM|MINE|I'M)\b/g

Which produces the following output:

YOU WENT TO THE MOVIES WITH YOUR FRIEND, SALLY. SALLY SAYS THAT YOU'M HER BEST FRIEND.

I'm very new to Javascript and, so, this is most probably a terrible way to do this and, of course this leaves me with the word YOU'M as part of my output as well.

But, ideally, I'd like a solution that could:

  • Allow for me to add new entries into the dictionary and they'd be added to the replacements (like was done here with the ${Object.keys(transforms).join('|')} portion)
  • Obviously, be able to deal with entries such as I'M.

What would be a good way to do this and, just in case anyone knows of a completely different way I could easily change from first to second person more easily / correctly, I'd LOVE that answer as well!!

Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Word boundaries won't give correct result because you have non-word characters like ' in I'M and since I is placed before the I'M in alternations \bI\b satisfies the match in I'm and makes it YOU'm.

To address this, you may use this solution with slightly different approach in regex i.e.

(?<!\w)(?:I|ME|MY|AM|MINE|I'M)(?![\w'])

RegEx Demo

  • (?<!\w): Negative Lookbehind to make sure we don't have a word character before the match
  • (?![\w']): Negative Lookahead to make sure we don't have a word character or ' after the match

Code:

function statementCreator() {
    var sentence = "I went to the movies with my friend, Sally. Sally says that I'm her best friend. I, the current narrator, went to the movies.";

    var transforms = {
        "I" : "YOU", 
        "ME" : "YOU", 
        "MY" : "YOUR", 
        "AM" : "ARE", 
        "MINE" : "YOURS",
        "I'M" : "YOU'RE"
    };

    var pattern = `(?<!\\w)(?:${Object.keys(transforms).join('|')})(?![\\w'])`;
    var re = new RegExp(pattern, "ig");

    str = sentence.toUpperCase().replace(re, matched => transforms[matched]);
    console.log(str);
}

statementCreator();

about 4 years ago · Juan Pablo Isaza Relatório

0

While whitespae bounaries will work when you are sure your words are always in between whitespace chars or start/end of string, a more universal solution in this case is simply sorting your keys by length in descending order before creating the alternation pattern:

Object.keys(transforms).sort((a, b) => b.length - a.length)

This fixes the problem because the first alternative found stops the regex engine from trying the rest of the alternatives, please refer to the "Remember That The Regex Engine Is Eager".

See the JavaScript demo:

function statementCreator() {
    var sentence = "I went to the movies with my friend, Sally. Sally says that I'm her best friend.";

    var transforms = {
        "I" : "YOU", 
        "ME" : "YOU", 
        "MY" : "YOUR", 
        "AM" : "ARE", 
        "MINE" : "YOURS",
        "I'M" : "YOU'RE"
    };

    var keys = Object.keys(transforms).sort((a, b) => b.length - a.length);
    var pattern = `\\b(?:${keys.join('|')})\\b`;
    var re = new RegExp(pattern, "g");

    str = sentence.toUpperCase().replace(re, matched => transforms[matched]);
    console.log(str);
}
statementCreator();

Output:

YOU WENT TO THE MOVIES WITH YOUR FRIEND, SALLY. SALLY SAYS THAT YOU'RE HER BEST FRIEND.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda