“JS Substitua” Respostas de código

Substituindo caracteres em String javascript

var myStr = 'this,is,a,test';
var newStr = myStr.replace(/,/g, '-');

console.log( newStr );  // "this-is-a-test"
Exuberant Emu

JavaScript Substitua tudo

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replaceAll('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"

// global flag required when calling replaceAll with regex
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
Matteoweb

JavaScript Substitua string

var str = "JavaScript replace method test";
var res = str.replace("test", "success");  
//res = Javscript replace method success
Batman

JS substitua os caracteres em uma string

var str = "Original string!";
var str = str.replace("Original", "New");
Poised Penguin

Substitua a string no TypeScript

var re = /apples/gi; 
var str = "Apples are round, and apples are juicy.";
var newstr = str.replace(re, "oranges"); 
console.log(newstr)
Lucky Louse

JS Substitua

// siingle replace
"str".replace('s', 'a') // atr
"str".replace(/s|r/, 'a') //ata

// replace all occurrences
"strstr".replace(/s/, 'a') //atratr

// replace with a function
function replacer(match, p1, p2, p3, offset, string) {
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}
let newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%
Anxious Alpaca

Respostas semelhantes a “JS Substitua”

Perguntas semelhantes a “JS Substitua”

Mais respostas relacionadas para “JS Substitua” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código