Steps to Reproduce:
Ctrl+H
([a-z])([A-Z])
$1-\L$1\E
expected fooBar
to be foo-bar
@elkebirmed May I know what are the standards/definitions for using \L and \E in your example?
@sandy081 Take a look
Let's say that we've this string:
Hello World
Capturing it with this RegEx: (Hello) (World)
Replacement is: \U1 \L2
will give us -> HELLO world
\U1 = $1 to Uppercase
\L2 = $2 to Lowercase
@elkebirmed Thanks for the documentation. This is cool to have. This is beyond the scope of what is currently supported by javascript.
We need to come up with our own advanced replace engine to support these cases.
Thanks
You're welcome @sandy081
It would be nice to have a way to supply a javascript function to the capture groups, so we can choose how to transform them and do more things than just upper/lower case. Since javascript already provides this, we just need an interface for it.
"some_VAR".replace(/([a-z]+)_([A-Z]+)/, function(match, p1, p2) {
return [p1.toUpperCase(), p2.toLowerCase()].join("_");
})
// "SOME_var"
Ran into this today, seems regex replacement still isn't quite there yet.
Ran into this today. Anyone know of an online tool which supports case conversion in regex replacement - just to save me having to install SublimeText just for this task, while waiting for VSCode to catch up?
@brianlowe Notepad++ will do what you need.
@retNull Thanks. Notepad++ does the casing transformation. (Now I just need to work up a suitably selective expression and corresponding substitution to complete my task.
I miss this from sublime :(
+1 for this functionality! I'm currently manually converting case on 100+ results in 50+ files... lol
As a temporary workaround, you can use a workflow like so:
_([a-zA-Z])
.$1
.upper
, then press ENTER.A bit of explanation added here:
https://gist.github.com/2a271f6440f1ac7e028df55e94035e40
@Ugotsta that workaround makes sense only to Windows users.
A more general workaround
_[A-Za-z]
This issue is over two years old and sounds really easy to fix and useful. Can we get an update or roadmap on this, please?
Is this possible, i.e. worth roadmapping, now that PCRE2 is an option, as of v1.29.0?
PCRE2's Replacement String Case Conversion
PCRE2 supports case conversion in replacement strings when using PCRE2_SUBSTITUTE_EXTENDED. \U converts everything that follows to uppercase. \L converts everything that follows to lowercase. \u converts the next character to uppercase. \l converts the next character to lowercase. \E turns off case conversion. As in Perl, the case conversion affects both literal text in your replacement string and the text inserted by backreferences.
Unlike in Perl, in PCRE2 \U, \L, \u, and \l all stop any preceding case conversion. So you cannot combine \L and \u, for example, to make the first character uppercase and the remainder lowercase. \L\u makes the first character uppercase and leaves the rest unchanged, just like \u. \u\L makes all characters lowercase, just like \L.
In PCRE2, case conversion runs through conditionals. Any case conversion in effect before the conditional also applies to the conditional. If the conditional contains its own case conversion escapes in the part of the conditional that is actually used, then those remain in effect after the conditional. So you could use ${1:+\U:\L}${2} to insert the text matched by the second capturing group in uppercase if the first group participated, and in lowercase if it didn't.
Really surprised to find out that this isn't supported in VSCode...this regex works great in Sublime Text for converting selected text to lowercase and converting spaces to dashes (slugify):
${TM_SELECTED_TEXT/([a-zA-Z]+)(?:(s+?)|\b)/?1:L$1(?2:-)E/g}
But doesn't work in VSCode?
Is this going to be something that is supported, or is there something I'm missing?
one of those features that you don't use much, but darn if it isn't obvious when not there.
+1 wanted this feature today
I wanted this feature in VS more than 15 years ago
Just open sublime and edit what u need to edit there I guess 🤷♂️
If you want to use vscode for this because you like the preview feature, you can use a workaround like the following for now:
([a-z])([A-Z])
replace with $1§§something§§$2
and then bulk replace in command line
shopt -s globstar
for f in **/*.{js,ts}; do sed -i -E 's/([a-z])§§something§§([A-Z])/\1_\L\2\E/g' "$f"; done
Better than switching the entire IDE for this ._.
@roblourens Does the ripgrep search support this capturing?
The replace still has to be executed on the front end.
Agreed
@sandy081 As @vbullinger said, a short note on when this might be added would be great.
Sorry, no particular time frame yet. But will update the milestone when it is planned.
This feature would be great mainly when importing lots of data from another source.
I've imported my database schema to make graphql schemas and needed this feature. I've use workarounds like the one proposed by @Ugotsta and worked, but it was as consuming as putting one by one there.
That said, it would be nice to have this feature :)
@jpike88 your solution worked. The case conversion command from the command palette isn't well known.
Spent a considerable amount of time trying to get \u$1 or \u1 to work for case conversion to figure out it's not even supported lol.
needed.
needed.
Me too
Frustrating because oddly enough this does seem to work on Windows VSCode. Was a bit surprised to hop over to Linux and find it failing. :(
@jimpriest I have not been able to get this working on Windows, so I'm curious what you've done?
@jimpriest I have not been able to get this working on Windows, so I'm curious what you've done?
@nemchik Well I'm pretty sure it used to work - I took notes in Jira ticket of what I used so I wouldn't forget.
find: var="(.*?)"
replace: var="\L$1\E"
Trying it now on my work PC (Windows) it doesn't work. :( I get the same results as I was seeing in Linux yesterday. My note was from Aug 2018 so maybe it's a version issue?
I'm not a +1
kind of commenter, but now I kind of wish I had on this thread just to see when I started tracking it. Earliest email I have on this thread was April 5th, but I know I've been using VSCode longer than that (at least since Oct 4, 2018, when generated a GitHub token to start using Settings Sync which was day 1 with VSCode for me I believe) and it's never been able to change case for me.
I would really appreciate if this feature was implemented as well.😊
^ Would be an awesome feature
I will also love this feature
I need this feature today, so I guess I'll have to install sublime or notepad++ 🤷♂
I need this feature today, so I guess I'll have to install sublime or notepad++ 🤷♂
There are existing solutions in the form of extensions in the marketplace
Would you be able to provide a link?
Would you be able to provide a link?
Text Transformer For VS Code
uppercase
change case
TextTransform
I don't remember remember which extension I use or if it's a feature now, the command I use from the command palette is 'Transform to'
Those extensions don't quite meet the need. Vscode already has case transformation natively, but something you can't do is regex find across multiple files and replace the found items with upper or lower case all at once.
Eg:
Search: \s+private final (\w+) (\w+);
Replace \t\u$2 \l$1 `json:"$2"`
Use case: Converting structures to go
I like this awesome feature, and sometimes I need to use this feature like replace all the tag name of html to lowercase if possible.
Is any updates for this issue?
That MS are not attending to this basic and important feature suggests that VSCode may be deprecated soon in favor of some other code editor. Shame, I quite liked VSCode.
suggests that VSCode may be deprecated soon in favor of some other code editor.
Somehow I doubt it. 😉
We could take a PR for this if anyone wants to work on it. I think we would want something basically consistent with another implementation: https://www.regular-expressions.info/replacecase.html but we can only support it for 'replace', not for searching with backreferences like Perl apparently supports. If someone wants to work on it, please let's talk about how it would work first 😁
Do you have a cost code we can invoice against?
With Regards,
Mr Sholto Maud
On 26 Feb 2020, at 05:16, Rob Lourens notifications@github.com wrote:
We could take a PR for this if anyone wants to work on it. I think we would want something basically consistent with another implementation: https://www.regular-expressions.info/replacecase.html but we can only support it for 'replace', not for searching with backreferences like Perl apparently supports. If someone wants to work on it, please let's talk about how it would work first 😁
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
Alt+Enter
+ F1
worked on my Ubuntu. Thanks to https://github.com/microsoft/vscode/issues/12185#issuecomment-414411370
It seems like not being able to change case using regex is an upstream issue. I hope by going through https://github.com/chmln/sd/issues/72#issue-596532078 & https://github.com/BurntSushi/ripgrep/issues/1546#issue-596389367 you will be able to understand what is the problem and what might be the solution.
It's not upstream, replacement is implemented in VS Code, not ripgrep.
@roblourens which regex flavour are you using for replacement?
@roblorens Search and replace is implemented differently in at least three places in VSCode.
As @blueray453 mentions, the global S&R seems to be implemented with ripgrep, which only "accidentally" supports replace and has no plans to add replacement features. So even if someone wanted to contribute the time to ripgrep, it likely wouldn't get merged. And the cost of revamping and unifying VSCode's various search implementations is a bit larger than most third-party PRs. And I'm not holding my breath. ;)
That said, I do have a draft PR #96128 (!) that I may spend more time on if there's interest (I'm not a Node guy). In my case I had mostly a single very long file I wanted to S&R, so "at least I'm okay". ;-)
Merged this for the find widget - work to do this for search is still ongoing.
Most helpful comment
This issue is over two years old and sounds really easy to fix and useful. Can we get an update or roadmap on this, please?