I usually do `str.replace(/./gs, c => dict[c] || c)`. It's a common idiom to replace chars based on a dictionary mapping. The regex I used guarantees that there's no conditional branching (because of "dot-all" flag), substantially increasing iteration performance. But be careful when iterating strings (in any way, not just the idiom I showed), because some JS functions iterate over code-units (split), code-points (spread syntax, and `for of` loops), or grapheme clusters (Intl.segmenter)
So it’s a cool example, thank you. But given the trend, that is typescript is now mostly used instead of JavaScript, how hard or how much more overhead would there be to write this in typescript? Just wondering.
Possibly, there are always other ways to do things. As I mentioned in another comment, this video is to demonstrate the usage of the replacer function but doesn't guarantee its the best way to do something
dude this could really be done in such a better way. instead of selecting all digits to begin with why not allow the regex to select only even numbers?
If it was up to me, yes I would do it that way but the point of this video is to show an example of how this function is used, not so much the intention of trying to only replace even numbers.
Every time I think I know JavaScript, I watch one of your seemingly benign videos and learn something new. Thanks!
I watch a video everyday after I find this channel
It is like a treasure vault for programming people
That's awesome, thanks!
blue background light is awsome. it suited so good
Powerful function with powerful explanation by powerful man.
Thanks for sharing.
🤣🤣 thank you!!
@@dcode-software welcome, really love your contents.
I am also js developer.
Being using replace for long time and I never knew about that callback function. Thank you for this treasure.
I usually do `str.replace(/./gs, c => dict[c] || c)`. It's a common idiom to replace chars based on a dictionary mapping. The regex I used guarantees that there's no conditional branching (because of "dot-all" flag), substantially increasing iteration performance.
But be careful when iterating strings (in any way, not just the idiom I showed), because some JS functions iterate over code-units (split), code-points (spread syntax, and `for of` loops), or grapheme clusters (Intl.segmenter)
Thanks for sharing!
You're welcome
Good stuff, but now I need a video on regular expressions.
So it’s a cool example, thank you. But given the trend, that is typescript is now mostly used instead of JavaScript, how hard or how much more overhead would there be to write this in typescript? Just wondering.
This video is a treasure trove!! Thanks man
Happy it helped you 🙂
Very useful. Thank you very much for sharing
Amazing!!!
Thanks need more video lile this 😌
Awesome video
Awesome, thank you! I plan to make more shorter videos highlighting lesser known parts of JavaScript so stay tuned 🙂
@@dcode-software plz make full playlist about DSA 😌
never knew. thanks.
Very cool replacer function 😎
I think so too!
Nice one!!, thank you I just liked your video
Great video! More please bro!
Will do 👀
Thnxs for new information
Cool video, thanks
No problem 👍
Amazing.
Glad you think so!
how is your cursor moving slowly from one character to another?
It's the VS Code setting which is called "cursorSmoothCaretAnimation"
Isnt using a map function better for these cases?
Possibly, there are always other ways to do things. As I mentioned in another comment, this video is to demonstrate the usage of the replacer function but doesn't guarantee its the best way to do something
+"3" turns string into number
Yes you could also do this 🙂
or multiply by 1, no? xD
How many times did he said "so" in this vídeo?
first?
dude this could really be done in such a better way. instead of selecting all digits to begin with why not allow the regex to select only even numbers?
If it was up to me, yes I would do it that way but the point of this video is to show an example of how this function is used, not so much the intention of trying to only replace even numbers.
@@dcode-software fair point.
{2024-03-25}