Категории

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

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

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/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!

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

Comments
  • shayan javadi
    shayan javadi 2 года назад myProfessors.replace(professor, Daniel Shiffman);
  • Stephen Curry
    Stephen Curry 2 года назад (изменено) myProfessors.replace(/myProfessors/, "Daniel Shiffman"); Should work now
  • exgc12
    exgc12 2 года назад God, thank you for your service Stephen Curry!
  • Stephen Curry
    Stephen Curry 2 года назад 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 10 месяцев назад 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 9 месяцев назад yeah, crazy how by accident it was the sum of the groups here too
  • Thành Long Nguyễn
    Thành Long Nguyễn 2 года назад 15 is position of the matched number
  • Mike & Drea
    Mike & Drea 2 года назад man you are hilarious hahaha 😂
  • Christian Rodier
    Christian Rodier Год назад Thanks, I think many coders struggle w/ regexp and could always use more help.
  • Leonard Ballok
    Leonard Ballok 6 месяцев назад very useful tutorials....I've spent 4 hours trying to solve a problem, after watching your video I solved it within 2 minutes !!!
  • Amirul Islam
    Amirul Islam 2 года назад Sorry no thumbs down :)
  • Kristian Blåsol
    Kristian Blåsol 2 года назад 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 2 года назад 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! :)
  • alexis arce gomez
    alexis arce gomez 3 месяца назад 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 2 месяца назад You should look into more useful stuff, like lookarounds, conditionals, etc. Not as fun as this series though. :)
  • Rron
    Rron 4 месяца назад You're doing Gods work, Shiffman.
  • Yuval Katzir
    Yuval Katzir Год назад me at my grandma's house 5:23
  • Kearfy
    Kearfy 4 месяца назад this is awesome 😂
  • Ryszard L
    Ryszard L Год назад this tutorial got me more excited than it is usually necessary
  • C Bro
    C Bro 2 года назад If you're using Atom, there's a setting called "Scroll Past End" under Preferences -> Editor. This is so you don't have to keep adding line breaks at the end of your document.
  • stillFLiP
    stillFLiP 2 месяца назад Yeah, I use vscodevim and I was just thinking to myself "press zz"
  • Ismar Šaćrović
    Ismar Šaćrović Год назад 2 minutes into the video and I found my error. Now THAT'S a good video :D bye
  • 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 :)
  • moaaz bhnas
    moaaz bhnas 2 года назад (изменено) It's my first time to complete an entire tutorial in one day without feeling bored .. you're awesome ❤️ I'm sorry .. my English is not good 😅
  • The Coding Train
    The Coding Train 2 года назад Love hearing that, and your English is great!
  • ahmed abohay
    ahmed abohay 2 года назад yup I LOVE HIS TUTORIALS TOOO
  • The Intellectuals
    The Intellectuals Год назад Your English is great stop doubting your self.
  • Csaba Balogh
    Csaba Balogh 2 года назад One of those rare videos on the internet that I want to like twice.
  • The Coding Train
    The Coding Train 2 года назад Thank you!
  • Dustin Gunnells
    Dustin Gunnells 6 месяцев назад OMG! This is useful!
  • Naseem Gh
    Naseem Gh Год назад 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.