Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:54:36+00:00 2026-05-23T23:54:36+00:00

I am trying to write a custom string splitting function, and it is harder

  • 0

I am trying to write a custom string splitting function, and it is harder than I would have expected.

Basically, I pass in a string and an array of the values that the string will split on, and it will return an array of substrings, removing empty ones and including the values it splits on. If the string can be split at the same place by two different values, the longer one has precedence.

That is,

split("Go ye away, I want some peace && quiet. & Thanks.", ["Go ", ",", "&&", "&", "."]);

should return

["Go ", "ye away", ",", " I want some peace ", "&&", " quiet", ".", " ", "&", " Thanks", "."]

Can you think of a reasonably simple algorithm for this? If there is a built-in way to do this in Javascript (I don’t think there is), it would be nicer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:54:37+00:00Added an answer on May 23, 2026 at 11:54 pm

    Something like this?

    function mySplit(input, delimiters) {
    
        // Sort delimiters array by length to avoid ambiguity
        delimiters.sort(function(a, b) {
           if (a.length > b.length) { return -1; }
           return 0;
        }
    
        var result = [];
    
        // Examine input one character at a time
        for (var i = 0; i < input.length; i++) {
            for (var j = 0; j < delimiters.length; j++) {
                if (input.substr(i, delimiters[j].length) == delimiters[j]) {
    
                    // Add first chunk of input to result
                    if (i > 0) {
                        result.push(input.substr(0, i));
                    }
                    result.push(delimiters[j]);
    
                    // Reset input and iteration
                    input = input.substr(i + delimiters[j].length);
                    i = 0;
                    j = 0;
                }
            }
        }
    
        return result;
    }
    
    var input      = "Go ye away, I want some peace && quiet. & Thanks.";
    var delimiters = ["Go ", ",", "&&", "&", "."];
    
    console.log(mySplit(input, delimiters));
    // Output: ["Go ", "ye away", ",", " I want some peace ",
    //          "&&", " quiet", ".", " ", "&", " Thanks", "."]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a custom function that will let me retrieve a cell
I'm trying to write a custom servlet (for AJAX/JSON) in which I would like
I'm trying to write my own custom View and I have problem with LayoutParams
I'm trying to write a custom validation method in my app - I have
I'm trying to write a custom WPF ValidationRule to enforce that a certain property
I am trying to write a custom tag. I can write attributes; however, I'm
I'm trying to write a custom rendering template for use in the DisplayForm of
I am trying to write a custom script that will keep a list of
I am trying to write a custom select statement in Hibernate using the getHibernateTemplate()
I am a threading noob and I am trying to write a custom thread

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.