Should String.prototype.split accept variable arguments optionally?

Benjamin (Inglor) Gruenbaum inglor at gmail.com
Wed Oct 16 09:51:42 PDT 2013


On Wed, Oct 16, 2013 at 7:15 PM, Rick Waldron <waldron.rick at gmail.com>
 wrote:

>
>This is subjective, as I have no trouble reading and understanding what
this means and is expected to do (also subjective).

Of course, this is another way to do it that does not require knowing
regular expressions. I have no doubt that being easier/harder is subjective
here.

> Since I've never actually found this to be a hardship, I'd be interested
in reading through the SO postings you mentioned above.

I don't keep a list :) Here is a bunch I just found:

- http://stackoverflow.com/q/650022/1348195
- http://stackoverflow.com/q/12504216/1348195
-
http://stackoverflow.com/questions/13867182/how-split-a-string-in-jquery-with-multiple-strings-as-separator?lq=1
-
http://stackoverflow.com/questions/19313541/split-a-string-based-on-multiple-delimiters?lq=1
- http://stackoverflow.com/q/4168644/1348195
-
http://stackoverflow.com/questions/9535203/split-string-by-whitespace-and-dashes

This is a _very_ shallow search, lots of the ones I run into are closed as
duplicates refering to the one with the many upvotes above. _Personally_ I
have no problem with regular expressions but many find them confusing in
such cases (I admit, a lot of those are language newbies I run into in JS
chats like that on Stack Overflow that just want JS to do something - but
they're a user base too).



On Wed, Oct 16, 2013 at 7:15 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

>
>
>
> On Wed, Oct 16, 2013 at 8:54 AM, Benjamin (Inglor) Gruenbaum <
> inglor at gmail.com> wrote:
>
>> Splitting by one value or another seems to be a pretty common use case if
>> Stack Overflow questions and personal experience are an indication. For
>> example "-" and " " and "/".
>>
>> Currently, the solution is to pass a regular expression to
>> String.prototype.split .
>>
>> However, it would be nice to be able to pass variable arguments to the
>> method.
>>
>> So if I want to split strings like "0-12-12" and "321+32/14" or "TI-CK
>> ER" instead of having to do:
>>
>> ```
>>     myString.split(/ |-|\/|\+/g); // this is no fun to read
>>
>
> This is subjective, as I have no trouble reading and understanding what
> this means and is expected to do (also subjective).
>
>
>>
>>     myString.split(" ","-","/","+"); // this is easier
>>     myString.split([" ","-","/","+"]); // this is also easier.
>>
>> ```
>>
>> The advantage of the second one (accepting an Array) is that it does not
>> require additional handling of the second "limit" parameter.
>>
>> A possible advantage of the first one is that it's simpler.
>>
>> The algorithm for the second one could be quite simple, in 21.1.3.17 only
>> _SplitMatch_ needs to be updated, and implementation sounds pretty simple
>> in engines and the semantics simple in the spec.
>>
>> Some other languages:
>>
>>  - C# accepts an array of `char` or array of "string" in its Split,
>> http://msdn.microsoft.com/en-us/library/tabh47cf.aspx
>>  - Ruby doesn't do this with `.split`, behaves like JS
>> http://ruby-doc.org/core-2.0.0/String.html#method-i-split
>>  - Java String.split only accepts a regex
>>  - Python doesn't do this with `.split`
>>
>> Advantages:
>>  - Nice api addition that solves a common use case.
>>  - A way to split by multiple delimiters easily without knowledge of
>> regular expressions.
>>  - Does not break the existing API, especially if we use array syntax.
>>
>> Disadvantages
>>  - Extra overhead
>>  - Use case needs assertion
>>
>> (Reminds me of stuff in string_extras
>> http://wiki.ecmascript.org/doku.php?id=harmony:string_extras )
>>
>>
>>
>>  What do you think?
>>
>
> Since I've never actually found this to be a hardship, I'd be interested
> in reading through the SO postings you mentioned above.
>
> Rick
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131016/1504d9ef/attachment.html>


More information about the es-discuss mailing list