Highlighted

Cant split by line breaks

Community Beginner ,
Aug 24, 2018

Copy link to clipboard

Copied

Hello,

I've looked everywhere for this, but I haven't had any results from suggestions in other discussions. I thought maybe it was somehow InDesign specific.

I'm trying to split a textframe's content by line breaks, in a file I didn't put together (and have no contact with the creator).

I initially tried

     split('\n');

with no luck. ( split('\t');     did work to split by tabs )

I have since tried everything I can think of / found online,

     split('\\n');

     split('\r\n');

     split('/\r\n|\n|\r/');

     split('<br>');

     split('</br>');

     split('');   ( getting desperate 😕 )

When I alert the contents it is split on two lines, so I know the content is broken up.

And when I enable 'show hidden characters' it does show a  ¶  character at the end of the lines I'm trying to split.

Does anyone know what character/code I should be using to split by?

Or is there somewhere in settings/preferences where I could specify the line break character for the document or paragraph style? ( I looked but couldn't find anything that sounded relevant)

This is slowly driving me crazy.

Any help would be very greatly appreciated!

I managed to get it to recognizes the paragraph return character!

In case it helps anyone else, here is the code that's working for me.

     split(/[\r\n]+/))

(with not surrounding quotation marks)

TOPICS
Scripting

Views

622

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Cant split by line breaks

Community Beginner ,
Aug 24, 2018

Copy link to clipboard

Copied

Hello,

I've looked everywhere for this, but I haven't had any results from suggestions in other discussions. I thought maybe it was somehow InDesign specific.

I'm trying to split a textframe's content by line breaks, in a file I didn't put together (and have no contact with the creator).

I initially tried

     split('\n');

with no luck. ( split('\t');     did work to split by tabs )

I have since tried everything I can think of / found online,

     split('\\n');

     split('\r\n');

     split('/\r\n|\n|\r/');

     split('<br>');

     split('</br>');

     split('');   ( getting desperate 😕 )

When I alert the contents it is split on two lines, so I know the content is broken up.

And when I enable 'show hidden characters' it does show a  ¶  character at the end of the lines I'm trying to split.

Does anyone know what character/code I should be using to split by?

Or is there somewhere in settings/preferences where I could specify the line break character for the document or paragraph style? ( I looked but couldn't find anything that sounded relevant)

This is slowly driving me crazy.

Any help would be very greatly appreciated!

I managed to get it to recognizes the paragraph return character!

In case it helps anyone else, here is the code that's working for me.

     split(/[\r\n]+/))

(with not surrounding quotation marks)

TOPICS
Scripting

Views

623

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 24, 2018 0
Community Beginner ,
Aug 24, 2018

Copy link to clipboard

Copied

I managed to get it to recognizes the paragraph return character!

In case it helps anyone else, here is the code that's working for me.

     split(/[\r\n]+/))

(with not surrounding quotation marks)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 24, 2018 2
Adobe Community Professional ,
Aug 24, 2018

Copy link to clipboard

Copied

Yes. string.split() also takes a regular expression as argument ( ExtendScript / JavaScript )

var s = "hi\nthere\n.";

s.split(/\n/);

// Result: [ "hi" , "there" , ". " ]

But I also get the same result with:

var s = "hi\nthere\n.";

s.split("\n");

Tested with ESTK 4.0.0.1.

This also works with InDesign's contents property of e.g. a text frame.

Even though the line break is a special character SpecialCharacters.FORCED_LINE_BREAK .

// Text frame selected

var s = app.selection[0].contents;

s.split("\n");

What is your string or contents you are running split() at?
If the contents of a text frame, can you show the text with hidden characters visible?

Best,
Uwe

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 24, 2018 0
Most Valuable Participant ,
Aug 24, 2018

Copy link to clipboard

Copied

I don't see the regular paragraph return \r  (single and on its own) in OP's own list of attempts. My guess the final combination [\r\n] only works because it is in there in the character class.

InDesign indeed may translate a certain set of characters to its own (extended) set, but it's usually not a problem when grabbing a larger string. You can select any single character and then check its Unicode in the Info panel, which indeed shows U+000D for the hard return, as well as for a smattering of other column and page breaks. The JavaScript-specific "\r" is merely a shorthand for this Unicode, just as "\n" for a Shift-Return and "\b" for a backspace.

(And with that last one the analogy breaks. "\b" does NOT translate to "backspace" in InDesign's native text; it is one of the special types of tab.)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 24, 2018 0
Community Beginner ,
Nov 12, 2020

Copy link to clipboard

Copied

Thank you so much for sharing. I was going crazy trying to figure why my expression seemed blind to the line breaks.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 12, 2020 0
Adobe Community Professional ,
Aug 24, 2018

Copy link to clipboard

Copied

Moved to InDesign Scripting.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 24, 2018 0