Jump to content

How can I tell the number of instances of a substring within a string?


Recommended Posts

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?

Link to comment
Share on other sites

You can use this to determine the number of break tags in a string:

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:

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>");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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;

Link to comment
Share on other sites

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


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




var titles = Rule("Your format rule");
var breaks = [color="red"]([/color]titles.match(/\<br\>/g)[color="Red"]||[])[/color].length; // Number of breaks
var email = "<br>static@emailaddress.com";
return (breaks < 4) ? title[color="red"]s[/color] + email : title[color="red"]s[/color] + email.replace("<br>","<br><br>");


The ||[] will cause the match to return null when there are no line-break tags.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...