2

I'm trying to split a string by either three or more pound signs or three or more spaces.

I'm using a function that looks like this:

     var produktDaten = dataMatch[0].replace(/\x03/g, '').trim().split('/[#\s]/{3,}');  
     console.log(produktDaten + ' is the data');

I need to clean the data up a bit, hence the replace and trim.

The output I'm getting looks like this:

##########################################################################MA-KF6###Beckhoff###EL1808    BECK.EL1808###MA-KF7###Beckhoff###EL1808    BECK.EL1808###MA-KF12###Beckhoff###EL1808    BECK.EL1808###MA-KF13###Beckhoff###EL1808    BECK.EL1808###MA-KF14###Beckhoff###EL1808    BECK.EL1808###MA-KF15###Beckhoff###EL1808    BECK.EL1808###MA-KF16###Beckhoff###EL1808    BECK.EL1808###MA-KF19###Beckhoff###EL1808    BECK.EL1808 is the data

How is this possible? Irrespective of the input, shouldn't the pound and multiple spaces be deleted by the split?

2
  • 6
    No, because you did not pass a regex. It should be .split(/[#\s]{3,}/). Are you seeking this output? Commented Jun 6, 2017 at 13:21
  • "either three or more pound signs or three or more spaces" then your regex should be /#{3,}|\s{3,}/. Your current regex would also split this "one## ##two" -> ['one', 'two'] Commented Jun 6, 2017 at 14:21

1 Answer 1

4

You passed a string to the split, the input string does not contain that string. I think you wanted to use

/[#\s]{3,}/

like here:

var produktDaten = "##########################################################################MA-KF6###Beckhoff###EL1808    BECK.EL1808###MA-KF7###Beckhoff###EL1808    BECK.EL1808###MA-KF12###Beckhoff###EL1808    BECK.EL1808###MA-KF13###Beckhoff###EL1808    BECK.EL1808###MA-KF14###Beckhoff###EL1808    BECK.EL1808###MA-KF15###Beckhoff###EL1808    BECK.EL1808###MA-KF16###Beckhoff###EL1808    BECK.EL1808###MA-KF19###Beckhoff###EL1808    BECK.EL1808";
console.log(produktDaten.replace(/\x03/g, '').trim().split(/[#\s]{3,}/));

This /[#\s]{3,}/ regex matches 3 or more chars that are either # or whitespace.

NOTE: just removing ' around it won't fix the issue since you are using an unescaped / and quantify it. You actually need to quantify the character class, [#\s].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.