6

I have a string (100*##G. Mobile Dashboard||Android App ( Practo.com )||# of new installs@@-##G. Mobile Dashboard||Android App ( Practo.com )||# of uninstalls@@

I want to split the string in such a way that it returns the following result (i.e it matches all characters that starts with ## and ends with @@ and splits the string with matched characters)

["(100*", "G. Mobile Dashboard||Android App ( Practo.com )||# of new installs", '-', 'G. Mobile Dashboard||Android App ( Practo.com )||# of uninstalls'
2
  • 1
    The initial string will have hyphen between those 2 characters. So when splitting the string with this specified characters, it needs to give hyphen in ouput Commented Aug 7, 2016 at 10:31
  • Sounds a lot like you just want to split on ## or @@. Commented Aug 7, 2016 at 10:34

3 Answers 3

8

Use String.prototype.split() passing a regex.

var str = "(100*##G. Mobile Dashboard||Android App ( Practo.com )||# of new installs@@-##G. Mobile Dashboard||Android App ( Practo.com )||# of uninstalls@@";

var re = /##(.*?)@@/;

var result = str.split(re);
console.log(result);

When you use Capturing parentheses in the regex, the captured text is also returned in the array.

Note that this will have an ending "" entry, because your string ends with @@. If you don't want that, just remove it.


  • If you always assume a well-formed string, the following regex yields the same result:

    /##|@@/
    

    *Commented by T.J. Crowder

  • If you expect newlines in between ## and @@, change the expression to:

    /##([\s\S]*?)@@/
    
  • If you need it to perform better, specially failing faster with longer strings:

    /##([^@]*(?:@[^@]+)*)@@/
    

    *Benchmark

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

4 Comments

Seems like the same result you get with /##|@@/, which would be simpler. (Assuming a well-formed string.)
@T.J.Crowder Except when you have unclosed structures, e.g. aaa##zzz. So it depends on what you expect in the input.
Heh, was just adding "...assuming a well-formed string." :-)
Haha... And I was adding the "...depends on what you expect..." :-)
4

You can first split by ##, then split each of the results by @@, and flatten the resulting array, like the following.

s.split('##').map(el => el.split('@@')).reduce((acc, curr) => acc.concat(curr))

Note that the last element of the resulting array will be empty string if you original string ends with @@, so you might need to remove it, depending on your use-case.

Comments

4

You can use:

var s = '(100*##G. Mobile Dashboard||Android App ( Practo.com )||# of new installs@@-##G. Mobile Dashboard||Android App ( Practo.com )||# of uninstalls@@'

var arr = s.split(/(##.*?@@)/).filter(Boolean)

//=> ["(100*", "##G. Mobile Dashboard||Android App ( Practo.com )||# of new installs@@", "-", "##G. Mobile Dashboard||Android App ( Practo.com )||# of uninstalls@@"]
  • Use a capturing group to get split text in resulting array
  • filter(Boolean) is needed to remove empty result from array

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.