Regex in scriptable

Hi all

Sorry for my newbie question (both JavaScript and Scriptable) but, how I use Regex on Scriptable?

I try two ways but not working:

  1. list.match(/^\A(.*)/)
  2. nl=/^\A(.*)/
    list.search(nl)

I want to extract the first line of my “list”. What I’m missing?

Thanks in advance

Can’t you use list[0] then?

My list is a multi line string, not an array

Not working, sorry

If you want the first line of a multiline string you can use

list.split("\n")[0]

Not working on scriptable but working good on other environments. Maybe the syntax is not correct. Scriptable shown this template when I write the code:

list.split(pattern?, limit?)

What error does it produce? Or what does it return?

The question mark behind the argument name means that the argument itself is optional. So you could technically call it without any arguments, but I currently don’t know what it then does…

First, split("\n")[0] definitely does work in Scriptable, as you can see if you run this short bit of test code:

a = `First line
Second line
Third line
Fourth line`;

b = a.split("\n")[0];
console.log(b);

So if it’s “not working” for you, the problem is likely to be either

  • The variable you’re applying split to doesn’t contain the text you think it does.
  • The text doesn’t use the linefeed character to separate lines.

Sometimes lines are separated by a combination of carriage return and linefeed characters; less common is a carriage return by itself. It depends on where the text came from. If the separator is the problem, there are ways to handle that.

But, as @schl3ck suggests, without seeing your code or the data it’s operating on, we don’t have enough information to help you.

Sorry but yes its works. I don’t know what happens the first time I try it

Thanks guys

or you can also write:

[b] = a.split("\n");

I’m unfamiliar with the [b] = construction. Is it basically a case that it’s throwing away every element of the list other than the first (which it stores in b)?

It’s a destructuring assignment of the array.

1 Like

Right. I guess the new-to-me bit is the idea you can just keep the first and throw away the rest.