What if I need to replace something with $? For example, replace '$var' with '$this->var=$var', the regex to search is easy, the problem is the replacement, I tries with '$this->$1 = $$1', it does not work properly. So how do we go about it?
First, you state you are using '$var', for replacement, this should be '(\$[a-zA-Z]*)'.
The parens allow for the selection as $1
The backslash allows VS Code to correctly find '$var' in the content as REGEX.
Second, to use '$this->$1 = $$1' as a pattern for replacement ...
The selection above in my first response would need changed to something like this '\$([a-zA-Z0-9]*)' to allow for selection of text that starts with a dollar sign.
The replacement pattern should be changed to '\$this->=>$1 = $$$1'. The dollar sign in the replacement is "escaped" using the double-dollar-sign (see HERE)
Having provided a reply, I think I'll write this up as a separate article for clarity. I'll post here when it's complete ... probably in the next day or two. Thanks for the GOOD question!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
What if I need to replace something with $? For example, replace '$var' with '$this->var=$var', the regex to search is easy, the problem is the replacement, I tries with '$this->$1 = $$1', it does not work properly. So how do we go about it?
Article written and posted ... here: dev.to/rfornal/vs-code-search-and-...
OK. I see a couple of things going on here.
First, you state you are using '$var', for replacement, this should be '(\$[a-zA-Z]*)'.
Second, to use '$this->$1 = $$1' as a pattern for replacement ...
Having provided a reply, I think I'll write this up as a separate article for clarity. I'll post here when it's complete ... probably in the next day or two. Thanks for the GOOD question!