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

silvioprog silvioprog at gmail.com
Mon Oct 7 15:46:12 CEST 2019


Oops, I forgot to send a patch. ^^'

Could you apply this one in attachment? The String.prototype.split() (and
many other string functions) allows to pass as *separator*(*) the most
powerful feature also presente in JS: regular expressions. After applying
the patch, it allows:

uses JS, Web;

var
  S: string;
  A: TStringDynArray;
begin
  S := 's:foo.bar';
  A := TJSString(S).split(TJSRegexp.New('[:.]'));
  console.log(A);
end.

Result:

(3) ["s", "foo", "bar"]

The equivalent version in pure JS (notice the regex part as parameter):

var s = 's:foo.bar';
var a = s.split(/[:\.]/);
console.log(a);

Result:

(3) ["s", "foo", "bar"]

Thank you again!

(*) Snip from MDN
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>
:

*"... Separator*
*Specifies the string which denotes the points at which each split should
occur. The separator is treated as a string or as a regular expression."*

On Mon, Oct 7, 2019 at 10:29 AM silvioprog <silvioprog at gmail.com> wrote:

> On Fri, Oct 4, 2019 at 6:33 PM Michael Van Canneyt <michael at freepascal.org>
> wrote:
>
>> Hm.
>> The documentation text does not mention the possibility of an array, so I
>> didn't add that. I have added an overload.
>>
>> Michael.
>
>
> Indeed, sometimes the MDN web docs lacks information on top and are
> present only on bottom (examples area).
>
> Thank you for solving! (y)
>
> --
> Silvio Clécio
>


-- 
Silvio Clécio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20191007/f87f67af/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: js.pas.diff
Type: text/x-patch
Size: 856 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20191007/f87f67af/attachment.bin>


More information about the Pas2js mailing list