I am trying to create a regex expression for the beginning of an object so I can replace the block complete with a single "
in vscode's find and replace.
My Regex
("id":{"\$oid":)[0-9]+(},"product_id":)[0-9]+(,")
My sample input
{"id":{"$oid":973},"product_id":973,"product_name":"Scotch - Queen Anne","product_amount":92,"product_group":"not perishable","distribution_to":"The Salvation Army Visalia - Neighborhood Market","distribution_from":"MCFB","expiration_date":"8/24/2020","pack_date":"4/19/2021","sell_by_date":"12/6/2020","use_by_date":"2/18/2021","created_at":"2020-04-24 03:15:40 -0400","updated_at":"2020-04-24 03:15:40 -0400"},
I have tested this expression on these websites with success: regex101.com, regexr.com, however, I still get an error when I paste into my vscode.
VSCode usageVscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.
Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.
Visual Studio uses . NET regular expressions to find and replace text.
How to enable VSCode regex replace. First, you need to press Ctrl + H on Windows and Linux, or ⌥⌘F on Mac to open up search and replace tool. In order to activate regex search and replace in VSCode, you have to click on the . * button near the input.
This is a valid regex, at least in the JavaScript engine used by VS Code (see this answer), but I think VS Code’s validation engine is confused (as was I) by the unescaped curly braces — the opening {
being in the first group and the closing }
in the second.
As suggested by the @rioV8 and @Nick in the comments you’ll need to escape them with the back slash \
to make it work:
("id":\{"\$oid":)[0-9]+(\},"product_id":)[0-9]+(,")
Here’s a little demo that demonstrates that the original regex works in JS:
const regex = /("id":{"\$oid":)[0-9]+(},"product_id":)[0-9]+(,")/;
const sample = '{"id":{"$oid":973},"product_id":973,"product_name":"Scotch - Queen Anne","product_amount":92,"product_group":"not perishable","distribution_to":"The Salvation Army Visalia - Neighborhood Market","distribution_from":"MCFB","expiration_date":"8/24/2020","pack_date":"4/19/2021","sell_by_date":"12/6/2020","use_by_date":"2/18/2021","created_at":"2020-04-24 03:15:40 -0400","updated_at":"2020-04-24 03:15:40 -0400"},';
const result = sample.match(regex);
let output = [
`full match:\t${result[0]}`,
`group 1:\t${result[1]}`,
`group 2:\t${result[2]}`,
`group 3:\t${result[3]}`
]
document.querySelector("#result").innerText = output.join('\n');
<pre id=result></pre>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With