VSCode正则表达式搜索和替换以删除代码块

时间:2020-01-01 03:12:36

标签: regex visual-studio-code

我们如何使用VSCode搜索和用正则表达式替换来删除带有模式的代码块

    id: {

      ...

      field: "id"
    },

例如,如果我们有以下代码:

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"
    },

搜索并替换后,它应该变成

module.exports = sequelize => {
  const attributes = {
    count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: null,
      comment: null,
      primaryKey: false,
      field: "count"
    },

尝试使用\t\t\tid: {*.? "id"\n\t\t\t},,但无法选择任何内容。

3 个答案:

答案 0 :(得分:2)

显然,VSCode会执行多行正则表达式only when they contain\n

所以这应该起作用:

\bid:(\n|.)+(?=count:)

(?=)是积极的前瞻性,因此可以找到“ count:”之前的所有内容。

答案 1 :(得分:1)

搜索:

\n\s+id: \{\s+[^}]*field: "id"[^}]*\},

并替换为空字符串。

https://regex101.com/r/rpOnch/1

您确实需要转义{括号,这对于正则表达式引擎来说是不常见的。

要修复原始样式,您需要

  • 逃脱括号

  • *.?替换为.*? 如果,您可以让点与换行符匹配,但是我看不到在VSCode中做到这一点的方法,因此您可能不得不改用[\s\S]*?(或类似的东西)

答案 2 :(得分:1)

您可以使用Select By扩展名。

基于2个正则表达式搜索所需的块。选择将被调整。按删除

相关问题