Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nam nulla fuga saepe neque asperiores! Quam tenetur harum ipsum dignissimos similique possimus esse soluta sunt eveniet aut, explicabo reprehenderit, eos ullam!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit, deserunt? Obcaecati non, vero odit ex soluta cupiditate pariatur accusantium sint porro, numquam autem cum cumque consectetur maiores inventore vitae et!
There are several ways to use change parts of a string in JS. The basic syntax ONLY replaces the first instance of the string:
let prose = prose.replace("twat", "fool");
The replaceAll()
method
The replaceAll()
method changes all instances of a string.
uses the new replaceAll
method.
function replaceMe() {
let firstName = prompt("whats yor name?");
alert("Welcome home " + firstName.replaceAll('e', 'a'));
}
The older way to replace all
According to A Smarter Way to Learn Javascript you replace all instances by changing the quotes in the word to remove with forward slashes and the letter g:
let fineProse = fineProse.replace(/fuckhead/g, "silly man");
This replaces the word fuckhead with silly man.
let poetry = poetry.replaceAll("farted", "broke wind");
uses the /first word/g
method.
function changeMe() {
let name = prompt("whats yor name?\n\(Never use yor reael name on the interent\)");
alert("Welcome home " + name.replace(/e/g, "a"));
}
Links
Up to date Article on different methods to replace by Dmitri Pavlutin