How can we use VSCode Search and Replace with Regular Expressions to remove a block of code with the pattern
id: {
...
field: "id"
},
For example, if we have the following code:
module.exports = sequelize => {
const attributes = {
id: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: "nextval(foo_id_seq::regclass)",
comment: null,
primaryKey: true,
field: "id"
},
count: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "count"
},
after Search and Replace, it should become
module.exports = sequelize => {
const attributes = {
count: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "count"
},
Tried using \\t\\t\\tid: {*.? "id"\\n\\t\\t\\t},
\\t\\t\\tid: {*.? "id"\\n\\t\\t\\t},
but it was unable to select anything.
Apparently VSCode does multi-line regexes only when they contain a \\n
:
So this should work:
\bid:(\n|.)+(?=count:)
(?=)
is a positive lookahead, so it finds everything before "count:".
Search for:
\n\s+id: \{\s+[^}]*field: "id"[^}]*\},
And replace with the empty string.
https://regex101.com/r/rpOnch/1
You do need to escape the {
brackets, which is unusual for a regex engine.
To fix your original pattern, you'd need to
escape the brackets
replace *.?
with .*?
if you could let the dot match a newline, but I don't see a way to do that in VSCode, so you probably have to use [\\s\\S]*?
(or something similar) instead
You can use the Select By extension.
Search for the block you want based on 2 regular expressions. Selection will be adjusted. Press Delete .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.