0

I have a long string containing text information about a multi-page image file. Here is an example:

test.tif[0] TIFF 1962x2668+0+0 DirectClass 1-bit 12.8M 0.000u test.tif[1] TIFF 
1952x2688+0+0 DirectClass 1-bit 12.8M 0.000u test.tif[2] TIFF 1650x2200+0+0 
DirectClass 1-bit 12.8M 0.000u 0:01 etc..

I need to convert the string into an array of image informations like this:

[
    'test.tif[0] TIFF 1962x2668+0+0 DirectClass 1-bit 12.8M 0.000u',
    'test.tif[1] TIFF 1952x2688+0+0 DirectClass 1-bit 12.8M 0.000u',
    'test.tif[2] TIFF 1650x2200+0+0 DirectClass 1-bit 12.8M 0.000u 0:01',
    etc..
]

Probably with some sort of regular expression it is possible to separate the part of the string starting with [x], including the name of the left side that can change every time. Or maybe there is a better way to do that ?

2
  • So you want to match from something.tif[somenumber] up to the next occurrence? Commented Mar 13, 2014 at 17:48
  • @MattBurland yes something.tif[somenumber], or even better something.someImageExtension[somenumber] – Cris69 11 mins ago Commented Mar 13, 2014 at 18:05

1 Answer 1

4
var array = string.split(/ (?=test\.tif\[\d+\])/);

This uses a lookahead to split on each space that is followed by test.tif[<number>].

To handle different extensions, you could change the tif portion of the regex to [a-zA-Z]+ to allow anything or something like (?:tif|gif|png) to allow only specific extensions.

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.