Joho the BlogBeginner to Beginner: Splitting strings into arrays in Javascript - Joho the Blog

Beginner to Beginner: Splitting strings into arrays in Javascript

I mentioned to my nephew Joel Weinberger, a CS grad student at UC Berkeley, that I wished the Javascript “split” method took multiple delimiters, and within minutes, he wrote one for me. If you know what I’m talking about, you can click here to get a zip file with the code (including a function as well as a method) and a sample. If you don’t …

[Note: all explanations are approximate.] Javascript comes with a built-in method for converting a string (that is, what normally consists of letters and characters in quotes) into an array (that is, a data structure of numbered elements). So, if you have a string that’s really a list of elements, such as “monday, tuesday, wednesday” or “12-345-6,” the split method will automatically chop it up into an array, using a delimiter of your choice (a comma or a dash in the two examples given). This is very useful.

But suppose you have a string such as this: “beef OR chicken AND duck” You want to be able to chop it up at the ORs and the ANDs, but the split method only lets you specify one delimiter.

Enter Joel. His multiSplit method lets you specify an array of delimiters. It chops up the string and records the phrases and their delimiters. Very handy.

Thanks, Joel!

[Tags: ]

4 Responses to “Beginner to Beginner: Splitting strings into arrays in Javascript”

  1. /(I am confused) ^ (you are in error)/

    You say:

    “but the split method only lets you specify one delimiter.”

    However, the documentation states:

    “Specifies the character, regular expression, or substring that is used to determine where to split the string”

    Multiple delimiters should be able to be handled with a regular expression. That is, roughly


    More complicated (use boundaries):


  2. Thanks, Seth. Joel thought “split” might take a regex, but the quick cheat sheet we consulted (we weren’t online so we used the one in the IDE I use) didn’t mention it.

    Joel’s has the advantage of retaining the info about which delimiter was used, which is of particular importance when you’re dealing with something like a simple boolean query, which was exactly what I wanted.

  3. Hey David,
    Do you mind if I use “all explanations are approximate” as a tagline for my blog? (I’d have to translate it to Norwegian.) It seems to capture something. I won’t credit you, as that would be quoting you out of context, and leave you wide open to accusations of adopting a relativist epistemology too. And I won’t take it personally if you say no, but I probably won’t buy your next book either. Nice work on the article above this, btw, I think professor Peterson has her adjectives and her nouns mixed up.
    Cheers, Karl Arthur

  4. Karl, of course you can quote me. I’m pleased. Feel free to link back to this post, but it’s totally up to you.

    — David W.

Web Joho only

Comments (RSS).  RSS icon