【问题标题】:bbedit - how to change multiple strings to title case?bbedit - 如何将多个字符串更改为标题大小写?
【发布时间】:2020-07-21 22:41:26
【问题描述】:

在 bbedit 中有一个功能,您可以选择文本,从菜单中选择 text->change case->make title case,它会相应地采取行动。

有没有办法在项目中的文件中选择多个文本字符串,然后应用相同的文本格式?

我知道你可以做一些正则表达式的东西并改变它,但这些都不是真正的标题案例,它忽略了“of”“和”“the”等词。标题案例很好,我只需要在很多项目上都这样做。

例如 5 个 html 文件有 <h2>THIS IS THE TITLE</h2> - 所以现在我去每个文件选择文本并执行上面的菜单项。如果有 5 个,那很好,但如果我想制作 2500 个 <h2>This is the Title</h2> - 那么我需要能够一次选择多个......

提前致谢!

-----编辑

因此,如果您在多个文件中搜索所有 <h2> 标记,并且您在不同的文件中返回多个文件.....

<h2>MY TITLE</h2>
<h2>this is a title</h2>
<h2>Another title</h2>

标题大小写会相应地将它们更改为:

<h2>My Title</h2>
<h2>This is a Title</h2>
<h2>Another Title</h2>

目前,您可以通过菜单单独选择每个选项。如果有意义的话,我们希望通过查找所有并更改大小写来做到这一点....

F:&lt;h2&gt;(.*?)&lt;/h2&gt; R:&lt;h2&gt;\1&lt;/h2&gt;[把这个当作一个特定的案例]

谢谢。

【问题讨论】:

  • atom 可以帮到你,不知道 bbedit
  • 请举出更多例子。什么应该和什么不应该匹配
  • 按要求添加编辑
  • 我们基本上想对所有特定字符串(通常在标签内)进行 grep 并使用 bbedit 批量更改大小写

标签: regex bbedit title-case


【解决方案1】:

几乎所有在 BBEdit 中涉及重复的操作都可以使用 AppleScript 自动化。 :-)

这是一个 AppleScript 脚本的文本,它将执行您描述的操作。您可以将其复制并粘贴到 AppleScript 编辑器中,并将其保存在 BBEdit 的“Scripts”文件夹中以备将来使用。

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "BBEdit"

    --  make sure we start at the top, because searches will (by default) proceed from the end of the selection range.
    tell text of document 1 to select insertion point before first character

    repeat
        tell text of document 1
            -- Find matches for a string inside of heading tags (any level)
            -- NOTE extra backslashes in the search pattern to keep AppleScript happy
            set aSearchResult to find "(<(h\\d)>)(.+?)(</\\2>)" options {search mode:grep}
        end tell

        if (not found of aSearchResult) then
            exit repeat -- we're done
        end if

        -- the opening tag is the first capture group. We'll use this below
        set openingTagText to grep substitution of "\\1"

        -- the title is the third capture group
        set titleText to grep substitution of "\\3"

        -- use "change case" to titlecase the title
        set changedTitleText to change case (titleText as string) making title case

        -- select the range of text containing the title, so that we can replace it

        set rangeStart to (characterOffset of found object of aSearchResult) + (length of openingTagText)
        set rangeEnd to (rangeStart + (length of changedTitleText) - 1)

        select (characters rangeStart through rangeEnd of text of document 1)

        -- replace the range
        set text of selection to changedTitleText
        --      select found object of aSearchResult
    end repeat

    --  put the insertion point back at the top, because it's a nice thing to do
    tell text of document 1 to select insertion point before first character

end tell

【讨论】:

  • 使用了类似的东西并添加到我们的工作组 npm 工具包中。感谢您的帮助。
猜你喜欢
  • 2019-07-20
  • 2010-11-24
  • 2012-03-04
  • 2010-11-15
  • 2021-03-30
相关资源
最近更新 更多