Created
February 22, 2022 12:57
-
-
Save ErickWendel/dab7ba63afc81117ffa42e3488cbd8cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const heroes = `NickName: Chapolin, Power: Hammer | |
Nick: Batman, Power: Money | |
` | |
const exp = /(NickName|Nick):\s(?<nickname>\w+),\sPower:\s(?<power>\w.*)/gm | |
const matchAll = [...heroes.matchAll(exp)].map(({ | |
groups: { | |
nickname, | |
power | |
} | |
}) => `${nickname} ${power}`).join('\n') | |
console.log(`1o\n${matchAll}`) | |
// 1o | |
// Chapolin Hammer | |
// Batman Money | |
const finalString = heroes.replace(exp, (s, _, nickname, power) => `${nickname} ${power}`) | |
console.log(`2o\n${finalString}`) | |
// 2o | |
// Chapolin Hammer | |
// Batman Money | |
const resultSingleLine = heroes.replace(exp, '$<nickname> $<power>') | |
console.log(`3o\n${resultSingleLine}`) | |
// 3o | |
// Chapolin Hammer | |
// Batman Money |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment