Code problem -- regular expression

I feel like I am SO close to solving this problem, but error shows “expected ‘DUBLON’ to equal ‘DUBLIN’”. Feel like I’m super close (: Is there anyone who can point me in the right direction of what to look for here? I’ve already looked at MDN (talk about confusing owners manual to the max lol) and W3Schools, but can’t seem to apply those explanations to this since they don’t explain how to change SEVERAL

function correct(string) {
  let answer = string.replace(/0|5|1/gi, "O","S","I");
  return answer;

That’s not how replace (or regex for that matter) works: regex is designed for looking for a specific pattern, replace is for taking that and replacing a match with a string

So you look for either 0, 1 or 5, and you replace any you find with “O”. replace only takes two arguments (a pattern and a replacement), so the other two are ignored.

Here is a setup for a functional solution, but note it’s probably clearer to just loop over the string and create a new string with the numbers replaced, rather than using regex (which is faffy and a bit nonintuitive for the purpose you’re trying to bend it to):

return string.replace(/0|1|5/g, (match) => {
  // `match` is going to be the *string* "0", "1"
  // or "5". So if it's "0" return "O", if it's "1" 
  // return "I" and so on

Note the “i” flag on the regex is unnecessary: numerals don’t have uppercase or lowercase.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.