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 Aug 7, 2016 at 10:31
  • Sounds a lot like you just want to split on ## or @@. 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

4
  • Seems like the same result you get with /##|@@/, which would be simpler. (Assuming a well-formed string.) Aug 7, 2016 at 10:34
  • @T.J.Crowder Except when you have unclosed structures, e.g. aaa##zzz. So it depends on what you expect in the input.
    – Mariano
    Aug 7, 2016 at 10:35
  • Heh, was just adding "...assuming a well-formed string." :-) Aug 7, 2016 at 10:36
  • 1
    Haha... And I was adding the "...depends on what you expect..." :-)
    – Mariano
    Aug 7, 2016 at 10:37
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.

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.