1

What I have is a string that I want to split.

I can set delimiters inside the string for example:

+++DELIMITER 1+++

text

+++DELIMITER R+++

text 2

+++NAME OF DELIMITER+++

text n

...

Edits after questions:

The string does not contain linefeed characters, An example of string wourld be:

let string = "+++DELIMITER 1+++ text +++DELIMITER R+++ text 2 +++NAME OF DELIMITER+++ specialchars \"£$%%£$\"<>";

text n";

What i want to obtain is an array constructed like this:

resultarray=[
     ["DELIMITER 1", "text"],
     ["DELIMITER R", "text 2"],
     ["NAME OF DELIMITER", "text n"]
     ...
];

I think I have to use String.split method, but I don't know what kind o f regex to use.

3
  • 3
    Can you please share a sample input and any attempts? The input you have shared is not a string variable Commented Aug 21, 2018 at 9:12
  • 1
    does your string has some kind of linefeed? Commented Aug 21, 2018 at 9:13
  • 1
    Try ^\+{3}([^+]+).*\s+((?:(?!^\+{3}).*\s*)*). See live demo here regex101.com/r/vuLOXg/1 Commented Aug 21, 2018 at 9:20

1 Answer 1

1

You could split the string and reduce single strings to pairs.

var string = '+++DELIMITER 1+++text+++DELIMITER R+++text 2+++NAME OF DELIMITER+++text n',
    parts = string
        .split(/\+{3}/)
        .slice(1)
        .reduce((r, s, i) => r.concat([i % 2 ? r.pop().concat(s) : [s]]), []);
    
console.log(parts);

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

1 Comment

i think it's a problem with op and the lack of some code.

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.