2.9: Regular Expressions: replace() – Programming with Text

In this video, I cover the JavaScript function replace(). The function allows you to search for a string (by matching a regular expression) and replace that string. The replacement can be a literal string itself or you can pass in a callback where you can use logic and programming to generate a replacement string based on anything you can imagine.

This is the ninth video in a series on Regular Expressions, which is part of the “Programming from A to Z” course at ITP (http://itp.nyu.edu).

Next Video: https://youtu.be/AKuW48WeNMA

http://shiffman.net/a2z/regex/

Course url: http://shiffman.net/a2z/

Support this channel on Patreon: https://patreon.com/codingtrain

Send me your questions and coding challenges!: https://github.com/CodingTrain/Rainbow-Topics

Contact: https://twitter.com/shiffman

GitHub Repo with all the info for Programming from A to Z: https://github.com/shiffman/A2Z-F16

Links discussed in this video:
MDN’s Regular Expressions Reference: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Regular Expressions on Wikipedia: https://en.wikipedia.org/wiki/Regular_expression
Book Mastering Regular Expressions: http://shop.oreilly.com/product/9780596528126.do
ITP from Tisch School of the Arts: https://tisch.nyu.edu/itp

Source Code for the all Video Lessons: https://github.com/CodingTrain/Rainbow-Code

p5.js: https://p5js.org/
Processing: https://processing.org

For More Programming from A to Z videos: https://www.youtube.com/user/shiffman/playlists?shelf_id=11&view=50&sort=dd

For More Coding Challenges: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH

Help us caption & translate this video!

http://amara.org/v/Vy9V/

📄 Code of Conduct: https://github.com/CodingTrain/Code-of-Conduct

source by The Coding Train

nodejs programming

Mourad ELGORMA

Fondateur de summarynetworks, passionné des nouvelles technologies et des métiers de Réseautique , Master en réseaux et système de télécommunications. ,j’ai affaire à Pascal, Delphi, Java, MATLAB, php …Connaissance du protocole TCP / IP, des applications Ethernet, des WLAN …Planification, installation et dépannage de problèmes de réseau informatique……Installez, configurez et dépannez les périphériques Cisco IOS. Surveillez les performances du réseau et isolez les défaillances du réseau. VLANs, protocoles de routage (RIPv2, EIGRP, OSPF.)…..Manipuler des systèmes embarqués (matériel et logiciel ex: Beaglebone Black)…Linux (Ubuntu, kali, serveur Mandriva Fedora, …). Microsoft (Windows, Windows Server 2003). ……Paquet tracer, GNS3, VMware Workstation, Virtual Box, Filezilla (client / serveur), EasyPhp, serveur Wamp,Le système de gestion WORDPRESS………Installation des caméras de surveillance ( technologie hikvision DVR………..). ,

37 réflexions sur “2.9: Regular Expressions: replace() – Programming with Text

  • juillet 22, 2021 à 5:05
    Permalien

    Very usefull, now I finally can say that I've learned about RegEx whit your tutorials series. Thank you very much teacher Danniel, regards from Mexico :-). YOU ARE THE BEST!!!

    Répondre
  • juillet 22, 2021 à 5:05
    Permalien

    Kittens and kittens and kittens and kittens and kittens…. 😍 Kittens and rainbows and cupcakes 😂

    Répondre
  • juillet 22, 2021 à 5:05
    Permalien

    Here's a useful way to get a Date object easily using RegExp.

    function getDate (str) {
    const matcher = /d{1,2}-d{1,2}-d{4}/;
    const match = matcher.exec(str);
    return new Date(match[3], match[2], match[1]);
    }

    Répondre
  • juillet 22, 2021 à 5:05
    Permalien

    Write down Regular Expressions that can extract Strings 123.jpg and 432.png from

    String axhdsjk123.jpg.jpg and hjhsd432.png.png

    How to?

    Répondre
  • juillet 22, 2021 à 5:05
    Permalien

    I used regex to seperate out half moves in chess to different capture groups from PGN files, to create a PGN viewer ( a chess game replayer) in JS 🙂 I love regex now, it saved me so much headache 😀

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    Thank you for your enthusiasm, you make learning a pleasant experience – even when i am learning after a long day at work, it's not a chore 🙂

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    Thnaks you very much sir.
    We appreciate your efforts.

    There is one query,
    Plz tell me how I can avoid html tags while replacing text on html page.

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    If a content or file contains "some value".I mean string like something is "fishy". How you match or handle tat quotes data with regex???

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    Hi..can someone help me on my problem…how can I sort, filter and set ranges columnwise on notepad ++.. I am struggling to find the right solution for this issue I am facing..thank you in advance..

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    ❤️❤️❤️❤️❤️👍👍👍👍

    This is very delicious what we learn here. 👨‍💻😋

    Exactly what I have needed.

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    nice videos! Here's another example that I just came up with of what you could do with replace. let names = {
    Marian: "Superhero",
    John: "Rescuer",
    };

    names = JSON.stringify(names);

    names = names.replace(/"(w+)":/g, `"new$1":`);

    console.log(JSON.parse(names)["newJohn"]);

    Whut you can do that with javascript? Niceee :)) or this:

    let names = {
    Marian: 2,
    John: 3,
    };

    names = JSON.stringify(names);

    console.log(names);

    names = names.replace(/:(d+)/g, function () {
    return `:${arguments[1] * 2}`;
    });

    What?? Ha ha this is too fun.

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    Hello, why does this work in splitting out numbers and text words per every capital letter?
    userNameStructure: any;
    const userName = '91MickeyMouse';

    this.userNameStructure = userName.replace(/([a-z0-9])([A-Z])/g, '$1 $2');

    console.log('words', this.userNameStructure);
    RESULT: words 91 Mickey Mouse

    My goal is to capture Mickey as first name, Mouse as last name. I was thinking I could call a function in place of '$1 $2', but that seems to fall apart.

    Répondre
  • juillet 22, 2021 à 5:04
    Permalien

    any advice how to split this string "n1->n2;n1->n3;goal<->n4;n4<-n1" to n1,n2,n3,n4,goal?

    Répondre

Laisser un commentaire