“JS Ganti” Kode Jawaban

Mengganti karakter dalam string javascript

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

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

JavaScript Ganti semuanya

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 mengganti string

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

JS menggantikan karakter dalam string

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

Ganti String di 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 Ganti

// 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

Jawaban yang mirip dengan “JS Ganti”

Pertanyaan yang mirip dengan “JS Ganti”

Lebih banyak jawaban terkait untuk “JS Ganti” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya