Meir Posted February 17, 2014 Share Posted February 17, 2014 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? Quote Link to comment Share on other sites More sharing options...
step Posted February 18, 2014 Share Posted February 18, 2014 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>"); Quote Link to comment Share on other sites More sharing options...
Meir Posted February 18, 2014 Author Share Posted February 18, 2014 I tried var str = Rule("Title Copyfit Rule"); // Titles if (str.match(/\<br\>/g).length == 4) return "<br>" else return "" Quote Link to comment Share on other sites More sharing options...
step Posted February 18, 2014 Share Posted February 18, 2014 Did that work? Quote Link to comment Share on other sites More sharing options...
Meir Posted February 18, 2014 Author Share Posted February 18, 2014 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. Quote Link to comment Share on other sites More sharing options...
step Posted February 18, 2014 Share Posted February 18, 2014 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; Quote Link to comment Share on other sites More sharing options...
David Miller Posted February 18, 2014 Share Posted February 18, 2014 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; Or 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. Quote Link to comment Share on other sites More sharing options...
Meir Posted February 18, 2014 Author Share Posted February 18, 2014 adding the parenthesis and ||[]) did the trick. Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.