Jakub
Jakub

Reputation: 2719

How in Visual Studio Code to use a regular expression to find a specific variable which has a number and replace that number to be a string

In my project, I have a situation in that some specific variables are assigned to numbers, but due to requirements, I have to change the numbers to a string.

For example, my original variable is like

question: 123

And I want to change it to

question: '123' 

However, the var name is always the same what changes is the number so the match should be like

where question: check if the number has '' if not add around the number the ''

I was wishing for a regular expression for it as I have so many variables to change that manually is not easy, but I'm not good with RegEx

I'm also open to different suggestions if any no need mandatory a RegEx if there is a better solution to my issue

Upvotes: 1

Views: 6445

Answers (1)

TomPst
TomPst

Reputation: 101

You can do this as following:

  1. Press Ctrl + H on Windows and Linux, or ⌥⌘F on Mac to enable the search and replace tool.
  2. Press the .* button to enable regex
  3. In the search field you can type the regex : question: ([0-9]+)
  4. In the replace field you can type : question: '$1'
  5. And replace all

Each occurence of question: <ANY NUMBER> will be replaced by question: '<ANY NUMBER>'

Upvotes: 4

Related Questions