【问题标题】:Getting Regex to work with custom Dreamweaver commands让 Regex 与自定义 Dreamweaver 命令一起使用
【发布时间】:2014-12-13 02:30:14
【问题描述】:

我有一个大型例行文档,需要多次正则表达式查找/替换搜索。为了加快这个过程,我考虑编写一个 Dreamweaver 命令来一次性执行我的所有搜索(参考:https://forums.adobe.com/thread/1193750)。

这听起来正是我所需要的,所以我开始使用该示例,到目前为止它对某些人来说效果很好,但是,我的 Regex 查找/替换被忽略了。关于自定义 Dreamweaver 命令的文档非常简短,我找不到任何得到很好回答的用例。我尝试在表达式周围添加/删除引号/正斜杠,但都没有运气。

我的正则表达式命令在正常的查找/替换中工作,但现在情况并非如此,因为它位于 Javascript 文件中。我使用带正则表达式的 Javascript 是否正确?有没有人幸运地让这样的事情起作用?

function canAcceptCommand() {
return true;
}

function commandButtons() {
    return new Array("Go!", "doIt()", "Cancel", "window.close()");
}

function doIt() {

var categoryTag = /<\/([A-Z]{2})>([^\r])<\1>/g;
dreamweaver.setUpFindReplace({
    searchString: categoryTag,
    replaceString: null,
    searchWhat: "document",
    searchSource: true,
    matchCase: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

dreamweaver.setUpFindReplace({
    searchString: "&",
    replaceString: "&amp;",
    searchWhat: "document",
    searchSource: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

var inlineRM = /(RM\s.*?,)/g;
var dropRM = /\n$1/;
dreamweaver.setUpFindReplace({
    searchString: inlineRM,
    replaceString: dropRM,
    searchWhat: "document",
    searchSource: true,
    matchCase: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

var incorrectAmPmFormat = /AM(\s-\s)(.*\s)PM/g;
var correctAmPmFormat = /a.m.$1$2p.m./;
dreamweaver.setUpFindReplace({
    searchString: incorrectAmPmFormat,
    replaceString: correctAmPmFormat,
    searchWhat: "document",
    searchSource: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

dreamweaver.setUpFindReplace({
    searchString: ":00",
    replaceString: "",
    searchWhat: "document",
    searchSource: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

dreamweaver.setUpFindReplace({
    searchString: "<fees aid:",
    replaceString: "\n<fees aid:",
    searchWhat: "document",
    searchSource: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

var removeBothPMs = /\sPM(\s-\s)(.*\s)PM/g;
var replaceWithOnePM = /$1$2p.m./;
dreamweaver.setUpFindReplace({
    searchString: removeBothPMs,
    replaceString: replaceWithOnePM,
    searchWhat: "document",
    searchSource: true,
    matchCase: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

var removeBothAMs = /\sAM(\s-\s)(.*\s)AM/g;
var replaceWithOneAM = /$1$2a.m./;
dreamweaver.setUpFindReplace({
    searchString: removeBothAMs,
    replaceString: replaceWithOneAM,
    searchWhat: "document",
    searchSource: true,
    matchCase: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

var threeSpaces = /\s\s\s<Image href="file:\/\/\/Volumes\/Communications\/assets\/New.eps"><\/Image>/;
dreamweaver.setUpFindReplace({
    searchString: "<new>NEW</new>",
    replaceString: threeSpaces,
    searchWhat: "document",
    searchSource: true,
    matchCase: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();

dreamweaver.setUpFindReplace({
    searchString: "><",
    replaceString: ">\n<",
    searchWhat: "document",
    searchSource: true,
    useRegularExpressions: true
});
dreamweaver.replaceAll();



   window.close();
}

我实际上还有更多要补充的,但如果它不能完全发挥作用,我不想在这方面投入太多时间。

感谢您的观看。

【问题讨论】:

    标签: javascript regex replace find dreamweaver


    【解决方案1】:

    所以,我放弃了 Dreamweaver。无论如何我都没有必要使用它(旧习惯应该消失)。对于那些仍在寻找解决方案的人,我建议采取类似的路线。我只是写了一个 Ruby 脚本(感谢:How to search file text for a pattern and replace it with a given value),它完全符合我的需要,但更优雅。 Dreamweaver 会在执行命令的负载下随机崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-21
      • 2017-07-27
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      相关资源
      最近更新 更多