3

有没有办法检查(在applescript中)列表(或html文本块)是否有starts with任意数量的值。

示例(检查单个值)

if {foobar starts with "<p>"} then
    -- do something awesome here
end if

除了我想传递多个值来检查<p><h1><em>

提前致谢。

4

2 回答 2

8
on startswith(txt, l)
    repeat with v in l
        if txt starts with v then return true
    end repeat
    false
end startswith

startswith("abc", {"a", "d", "e"}) -- true
于 2011-05-08T20:42:51.133 回答
5

如果你想保持 AppleScript 的“英语”风格,虽然比上面的例子长,你可以这样做:

if {foobar starts with "hello" or foobar starts with "goodbye"} then

一个完整的例子是:

set foobar to "hello dude"
if {foobar starts with "hello" or foobar starts with "goodbye"} then
    display dialog "found"
end if

即使您更改,这也是正确的:

set foobar to "hello dude"

至:

set foobar to "goodbye dude"
于 2011-05-11T07:08:21.510 回答