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

Опубликовано: 19 сент. 2016 г. 24 247 просмотров

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


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

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...
Regular Expressions on Wikipedia: https://en.wikipedia.org/wiki/Regular...
Book Mastering Regular Expressions: http://shop.oreilly.com/product/97805...
ITP from Tisch School of the Arts: https://tisch.nyu.edu/itp

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

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

For More Programming from A to Z videos: https://www.youtube.com/user/shiffman...

For More Coding Challenges: https://www.youtube.com/playlist?list...

Help us caption & translate this video!


  • shayan javadi
    shayan javadi 2 года назад myProfessors.replace(professor, Daniel Shiffman);
  • Stephen Curry
    Stephen Curry Год назад (изменено) myProfessors.replace(/myProfessors/, "Daniel Shiffman"); Should work now
  • exgc12
    exgc12 Год назад God, thank you for your service Stephen Curry!
  • Stephen Curry
    Stephen Curry Год назад No problem. Always here to help
  • vladanng
    vladanng Год назад You should be specialized on curying Stephen :)
  • Nitish Puri
    Nitish Puri Год назад You forgot the global flag myProfessors.replace(/myProfessors/g, "Daniel Shiffman");
  • Avago24
    Avago24 8 месяцев назад wrong! replace does not change the original string! myProffessors = myProffessors.replace(/myProffesors/g, 'Daniel Shiffman'); this works, tested
  • jumpmancw
    jumpmancw 2 года назад just so people know, the number 15 at the end is not the groups added together, it is the index of where the match starts in the string. If you match the first character of the string, that index will be 0.
  • K4kshi
    K4kshi 8 месяцев назад yeah, crazy how by accident it was the sum of the groups here too
  • Mike & Drea
    Mike & Drea 2 года назад man you are hilarious hahaha 😂
  • alexis arce gomez
    alexis arce gomez 1 месяц назад 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!!!
  • stillFLiP
    stillFLiP 3 недели назад You should look into more useful stuff, like lookarounds, conditionals, etc. Not as fun as this series though. :)
  • Christian Rodier
    Christian Rodier Год назад Thanks, I think many coders struggle w/ regexp and could always use more help.
  • Rron
    Rron 2 месяца назад You're doing Gods work, Shiffman.
  • Kristian Blåsol
    Kristian Blåsol Год назад Just watched this whole series and it was mind boggeling :) Thank you for teching me Regular expressions, and I've been coding so long, and now I dont know how I can have lived without RegEx... :D Anyways, one question or problem to solve. When parsing a text for hashtags (#) on social media, I found that this: /(#\w+)/g works for finding any word that starts with a hash, BUT it also finds hashes that you might have in a normal url with an anchor. I know these arent frequently used anymore, but still, Id like to not find hashes inside other texts. And no matter how many \b I use or where I cant get it to ignore those. Adding a \s first kinda works, but then messes up rows that begin with a hashtag: here it is in action: http://regexr.com/3fkmn
  • The Coding Train
    The Coding Train Год назад Great question! I think \b probably doesn't work b/c a slash is maybe a valid word boundary. Maybe you could try \s or ^ (beginning of line)?
  • Minh Trí Cao
    Minh Trí Cao Год назад \B(\#\w+)\b should be the answer, but it doesn't solve the case; .......?#............
  • Kristian Blåsol
    Kristian Blåsol Год назад Thank you Minh, this actually seems to work much better! :)
  • Thành Long Nguyễn
    Thành Long Nguyễn 2 года назад 15 is position of the matched number
  • Amirul Islam
    Amirul Islam 2 года назад Sorry no thumbs down :)
  • Leonard Ballok
    Leonard Ballok 4 месяца назад very useful tutorials....I've spent 4 hours trying to solve a problem, after watching your video I solved it within 2 minutes !!!
  • Yuval Katzir
    Yuval Katzir Год назад me at my grandma's house 5:23
  • Kearfy
    Kearfy 2 месяца назад this is awesome 😂
  • Dustin Gunnells
    Dustin Gunnells 4 месяца назад OMG! This is useful!
  • Loïc Bertrand
    Loïc Bertrand 1 месяц назад Kittens and kittens and kittens and kittens and kittens.... 😍 Kittens and rainbows and cupcakes 😂
  • Ryszard L
    Ryszard L Год назад this tutorial got me more excited than it is usually necessary
  • Ismar Šaćrović
    Ismar Šaćrović 11 месяцев назад 2 minutes into the video and I found my error. Now THAT'S a good video :D bye
  • kandysman86
    kandysman86 3 месяца назад This video just triggered a Eureka moment in my brain. I'm 35 and just started learning coding 4 months ago and I think I've just turned a corner. Thank u man. I'm not sure what u did, but it's like a fog has just lifted off of javascript for me.
  • The Coding Train
    The Coding Train 3 месяца назад I'm glad to hear!
  • Web Koder
    Web Koder 3 месяца назад I would like to see you in strict mode
  • Naseem Gh
    Naseem Gh 11 месяцев назад that was an amazing tutorial, i didn't even noticed that i'm watching the last one. in few hours i watched all your videos and i'm so thankful to learn from you. greetings from Syria you are the best.
  • Oscar van Velsen
    Oscar van Velsen 2 года назад This dude has way less views than he deserves... Imma help U out and push your channel among my geek friends :)