Hello!

So I’m brand new to perchance, so please make sure to explain things to me very thoroughly.

I’m trying to make a random genotype generator, that then looks at what was generated and replaces it with words for the phenotype.

I’ve got it figured out for the most part, but what baffles me is that stringing .replace together for multiple instances (maybe I’m doing it wrong), does not have each replace function look at the entire string.

I’ve done quite a bit of googling, and found a few things similar to my issue, but could not understand them enough to implement the fixes.

As a word/name example: I have outputs of “Mary” and “Marybeth”. I want Mary replaced with “Anne”, and Marybeth replaced to “Susan”.

However, what I’m essentially getting is Anne and Annebeth, with no reference to Susan whatsoever (but in my actual use-case, a number tacked on the end of a string of letters).

Is there a way to force the replace function to only replace characters if the entire bit in quotations matches, and not replace partial matches?

  • Selena@lemmy.worldOP
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 month ago

    Aaand found a solution on the discord. Which I’ll post here in case anyone else finds this thread and needs help.

    Solution: .replace(/^word$/, ‘replace’) where ^ denotes the start of the word/line and $ denotes the end of the word/line.

    • Vector@lemmy.world
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 month ago

      For further context, the above makes use of a “regular expression”. These can also be used to perform powerful match-based search operations with special syntax. Sometimes they’re referred to as “regex” or “regexp”, and there’s plenty of info available through your favorite search engine if you want to learn about all of the neat things you can do with them.