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

  • SEARCH
  • Home
  • 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 9027621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:47:19+00:00 2026-06-16T06:47:19+00:00

I have some function that uses split, but I want the separator to be

  • 0

I have some function that uses split, but I want the separator to be a variable. separator will be used several places and it would be nice to only have to change the variable once rather than change hard code everywhere.

But, of course, it doesn’t work. The pattern looks correct, but split must have some problem with variables defining the split.

Here is some testing I did in Google Chrome’s console.

separator = '|'; // separator to use
var pattern = '/\\' + separator + '\\s*/'; // define pattern
undefined
pattern;
"/\|\s*/"
// pattern is correct
// now define function with constant... variable version commented:
function split( val ) {
        //var pattern = '/\\' + separator + '\\s*/'; 
        return val.split( /\|\s*/ );
        //return val.split( pattern );
    }
undefined
split('asdf|jkl');
["asdf", "jkl"]
// function worked as expected
// now uncomment the variable portions:
function split( val ) {
        var pattern = '/\\' + separator + '\\s*/'; 
        //return val.split( /\|\s*/ );
        return val.split( pattern );
    }
undefined
split('asdf|jkl');
["asdf|jkl"]
// not as expected. string is not split into array

I assume this has something to do with the split function. How do I get this to work? Note that the only reason I think I need to do it this way is because I want to easily be able to change separator. If not for that, I could just hard code everything it would work.

EDIT:
I don’t care if it uses regex or not. But it needs to split the string on separator and also make sure the values are trimmed of extra spaces.

Also, I’m modeling my code after the jQuery UI page for autocomplete, here.
And here’s the function as they have it. I don’t know why they define their own split.

function split( val ) {
     return val.split( /,\s*/ );
}
  • 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-06-16T06:47:21+00:00Added an answer on June 16, 2026 at 6:47 am

    Concat the string, and use it to create an instance of a new RegExp obj.

    function split( val ) {
            var pattern = new RegExp('\\' + separator + '\\s*'); 
            return val.split( pattern );
    }
    

    EDITS

    Note that in this particular example, there doesn’t appear to be any value over String.prototype.split().

    This would be more useful if you wanted to set a limit for all splits of a certain type:

    function split( val ) {
            var pattern = new RegExp('\\' + separator + '\\s*'); 
            return val.split( pattern , 3);
    }
    

    without having to write:

    strVar.split(pattern, 3);
    

    Yes, that limit can be stored as a variable and referenced, but then you have to expose that variable throughout the scope of every function you want to use it in.

    What if you wanted to change it so it would take an array of strings to split, instead?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write a function that would have some optional code to be
I have a function that will parse some data coming in. My problem is
I have this function that I would like to condense into some iterator. How
I have some code that uses the stristr function to extract data I need.
I have a function that uses the current time to make some calculations. I'd
I'm using Grails 1.3.7. I have some code that uses the built-in base64Encode function
I have some trouble forward declaring a function that uses boost::enable_if : the following
I have a function that uses runif to calculate some value, so each time
I have some inherited JS code that uses this format: function main(param) { var
I have some code that uses multi-methods and would ideally like to overload the

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.