๐str.replace()
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
JS โฉ value โฉ primitive โฉ String โฉ method โฉ .replace()
// quoting style: single (') -> double (")
let text = "'I'm the cook,' he said, 'it's my job.'";
text.replace(/(^|\W)'|'(\W|$)/g, '$1"$2'));
// โฐโ1โโโฏ โฐโ2โโโฏ
// โญ๏ธ Groups that are not matched will be replaced by nothing.
// โ "I'm the cook," he said, "it's my job."
๐ RegExp โฉ replace pattern
๐็ฏไพ๏ผ
minus one - replacer function example.
codepen โฉ str.replace()
// โญ๏ธ replacement string
// replacement string โด
// โญ1โโฎ โญโ2โโฎ โญ 1 โฎ โญ 2 โฎ โญโโโโโโโฎ
let str1 = 'John Smith'.replace(/(\w+)\s(\w+)/, '$2, $1');
// โฐ match โโฏ โญโ2โโฎ โญ1โโฎ
log(str1); // "Smith, John"
// p1: nondigits