-2

I'm trying to replace %username% with any name inputted. I wanna replace that keeping the end part of the entire string %username%, your Account Has Been Successfully Created!

<h2 class="done">%username%, your Account Has Been Successfully Created!</h2>
let name = $(".done");
name.text().replace(/\%(.+?)\%/, this.value);

this.value is just the username inputted in the input field.

Zesty
  • 273
  • 2
  • 6
  • 19
  • You are altering the string, it does not magically get applied back to the element. You need to set the element's text with the new value. – epascarello Jun 29 '22 at 01:53

1 Answers1

1

You need to assign the replaced string back to the original variable, e.g.

let name = $(".done");
name.text(name.text().replace(/%.+?%/, this.value));

Note that % is not a regex metacharacter, and so it does require any escaping with backslash.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360