Applescript and "starts with" operator
Is there a way to check (in apple开发者_高级运维script) if a list (or block of html text) starts with
any number of values.
Example (checking for a single value)
if {foobar starts with "<p>"} then
-- do something awesome here
end if
except i would like to pass multiple values to check <p>
or <h1>
or <em>
.
Thanks in advance.
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
If you want to stay within the 'English' style of AppleScript, although longer than the example above, you can just do this:
if {foobar starts with "hello" or foobar starts with "goodbye"} then
A full example would be:
set foobar to "hello dude"
if {foobar starts with "hello" or foobar starts with "goodbye"} then
display dialog "found"
end if
That will be true even if you change:
set foobar to "hello dude"
to:
set foobar to "goodbye dude"
精彩评论