[Pas2js] How to split a string using an array as separator?

Michael Van Canneyt michael at freepascal.org
Fri Oct 4 23:33:20 CEST 2019



On Fri, 4 Oct 2019, silvioprog wrote:

> Hi.
>
> In pure JS, we can split a string using an array as separator (please see
> "Splitting with an array as separator" here
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>),
> e.g.:
>
> var s = 's:foo.bar';
> var a = s.split(/[:\.]/);
>
> console.log(a);
> (3) ["s", "foo", "bar"]
>
> So, is there any trick to do something like this in pure Pascal? I tried:
>
> A := TJSString(S).split([':', '.']); // error at transpile time
> A := TJSString(S).split(':', '.'); // error at transpile time
> A := TJSString(S).split('/[:.]/'); // the JS got only a static string
>
> Since the TJSString.split() signature allows only a static string as first
> parameter, calls like those above don't work at all, then as workaround I
> solved it using an asm block, however, it would be nice to allow pure
> Pascal in that.

Hm. 
The documentation text does not mention the possibility of an array, so I
didn't add that. I have added an overload.

Michael.


More information about the Pas2js mailing list