Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old February 17th, 2014, 03:42 PM
Meir Meir is offline
Regular Contributor
 
Join Date: Mar 2011
Location: Lawrenceville, NJ
Posts: 50
Default How can I tell the number of instances of a substring within a string?

I have customers entering up to three titles. Occasionally they add a linebreak tag to have more control on appearances.

Currently, there is an email field in a static position, but they want it to shift down a line only if the titles take up 4 lines.

I have a rule that outputs the all the titles formatted as one variable, and breaks those lines with a linebreak tag.

How can I count the number of instances of "<br>" in the output string?
__________________
Meir Galimidi
Allegra
FP 7.2P1k, MAC OSX 10.5.8, Acrobat 9.4.0
Reply With Quote
  #2  
Old February 18th, 2014, 08:34 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: How can I tell the number of instances of a substring within a string?

You can use this to determine the number of break tags in a string:
Code:
var str = Rule("Your format rule"); // Titles
return str.match(/\<br\>/g).length;
Or you could assign that to a variable and move your static text like so:
Code:
var titles = Rule("Your format rule");
var breaks = titles.match(/\<br\>/g).length; // Number of breaks
var email = "<br>static@emailaddress.com";
return (breaks < 4) ? title + email : title + email.replace("<br>","<br><br>");
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #3  
Old February 18th, 2014, 09:46 AM
Meir Meir is offline
Regular Contributor
 
Join Date: Mar 2011
Location: Lawrenceville, NJ
Posts: 50
Default Re: How can I tell the number of instances of a substring within a string?

I tried

[CODE]
var str = Rule("Title Copyfit Rule"); // Titles
if (str.match(/\<br\>/g).length == 4)
return "<br>"
else return ""
__________________
Meir Galimidi
Allegra
FP 7.2P1k, MAC OSX 10.5.8, Acrobat 9.4.0
Reply With Quote
  #4  
Old February 18th, 2014, 09:58 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: How can I tell the number of instances of a substring within a string?

Did that work?
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #5  
Old February 18th, 2014, 10:32 AM
Meir Meir is offline
Regular Contributor
 
Join Date: Mar 2011
Location: Lawrenceville, NJ
Posts: 50
Default Re: How can I tell the number of instances of a substring within a string?

Ahh sorry. I forgot it cuts off everything you write after and end code block.

No it did not work. I am getting an error when there are either no titles or the title(s) can all fit on one line:

TypeError: str.match(/\<br\>/g) has no properties)

I need to edit the code to be able to continue if there are no properties. Right now that if statement errors out.
__________________
Meir Galimidi
Allegra
FP 7.2P1k, MAC OSX 10.5.8, Acrobat 9.4.0
Reply With Quote
  #6  
Old February 18th, 2014, 11:38 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: How can I tell the number of instances of a substring within a string?

Is that the error you get at validation? Does it preview/compose correctly?

You could add a try/catch statement when defining your variables so that if the match function can't find any breaks, it will default to being defined as 0.
Code:
try {var breaks = Rule("Title Copyfit Rule").match(/\<br\>/g).length;} catch(e){var breaks = 0;}
var result = "";

if (breaks == 4) 
    result = "<br>";

return result;
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #7  
Old February 18th, 2014, 11:46 AM
David Miller David Miller is offline
Senior Contributor
 
Join Date: Oct 2008
Location: Wallingford, CT
Posts: 263
Default Re: How can I tell the number of instances of a substring within a string?

I believe you can edit Step's original code as well. Edits are in Red.

Code:
var str = Rule("Your format rule"; // Titles
return (str.match(/\<br\>/g)||[]).length;
Or

Code:
var titles = Rule("Your format rule");
var breaks = (titles.match(/\<br\>/g)||[]).length; // Number of breaks
var email = "<br>static@emailaddress.com";
return (breaks < 4) ? titles + email : titles + email.replace("<br>","<br><br>");
The ||[] will cause the match to return null when there are no line-break tags.
Reply With Quote
  #8  
Old February 18th, 2014, 02:14 PM
Meir Meir is offline
Regular Contributor
 
Join Date: Mar 2011
Location: Lawrenceville, NJ
Posts: 50
Default Re: How can I tell the number of instances of a substring within a string?

adding the parenthesis and ||[]) did the trick.

Thank you.
__________________
Meir Galimidi
Allegra
FP 7.2P1k, MAC OSX 10.5.8, Acrobat 9.4.0
Reply With Quote
Reply

Tags
linebreak, string, substring

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 08:14 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.