I notice a lot of code where people do something like:
myClass.someMethod(something here, $1);
The $1 is picking up a value from “something here”?
What is this known as? I can’t seem to find it anywhere? But this step, process is used in cases with regex quite a bit..
You’ll often see this in regular expressions where
$1represents a capture group that you’re carrying over into the new value.For example, suppose we’re building a tweet-parser for our website. We want to find @ references in the tweet, and convert them into links to those particular accounts:
Note here that we’re matching every occurrence of @something, but we’re wrapping the username portion in
(and)so that we can handle it individually in our replacement text. The entire pattern is represented by$0, which will hold the value in its entirety from@to the last char in the username.The same is true for JavaScript:
The variable
linksnow contains the value:You might note that I used
$&in JavaScript to grab the entire matched pattern while using$0in PHP – we have to deal with these differences in life. My apologies 😉