blyons Posted March 23, 2016 Share Posted March 23, 2016 We use FusionPro with our web templates product and are having trouble. The words of, and, at, and for, need to be in italics So it would read: Department of English- and the (*) allows for a line break if necessary. However if we package this and test and use any 2 of the words together (of, and, at, or for) it won’t give a space between them ex: Department ofthe English. var cod = (Trim(Field("COD"))); cod = ReplaceSubstring(cod," of ",' <i>of</i> '); cod = ReplaceSubstring(cod," the ",' <i>the</i> '); cod = ReplaceSubstring(cod," of the ",' <i>of </i> <i>the</i> '); COD = ReplaceSubstring(cod,"*","<br>"); return cod; } Thanks so much in advance for your assist! Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted March 23, 2016 Share Posted March 23, 2016 (edited) You need to replace the spaces after the tags with   entities, like so: cod = ReplaceSubstring(cod," of the ",'&[size="3"]#[/size]32;<i>of</i>&[size="3"]#[/size]32;<i>the</i>&[size="3"]#[/size]32;'); cod = ReplaceSubstring(cod," of ",'&[size="3"]#[/size]32;<i>of</i>&[size="3"]#[/size]32;'); cod = ReplaceSubstring(cod," the ",&[size="3"]#[/size]32;<i>the</i>&[size="3"]#[/size]32;'); cod = ReplaceSubstring(cod,"*","<br>"); return cod; Or, more generally: var wordsToItalicize = ["of", "the"]; for (var w in wordsToItalicize) cod = cod.replace(new RegExp("\\b(" + wordsToItalicize[w] + ")\\s\\b", "gi"), "<i>$1</i>&[size="3"]#[/size]32;"); return cod; Edited March 23, 2016 by Dan Korn Added \b word boundaries to not capture partial words. Quote Link to comment Share on other sites More sharing options...
blyons Posted March 23, 2016 Author Share Posted March 23, 2016 Dan, I cannot thank you enough :):) it worked! seriously you achieved hero status in our office! Quote Link to comment Share on other sites More sharing options...
blyons Posted March 24, 2016 Author Share Posted March 24, 2016 This worked out great. but we came across another issue-we do need an exception to the rule if "the" is the first word. We want to be able to capitalize and not auto italic. Quote Link to comment Share on other sites More sharing options...
step Posted March 24, 2016 Share Posted March 24, 2016 This worked out great. but we came across another issue-we do need an exception to the rule if "the" is the first word. We want to be able to capitalize and not auto italic. This will keep the regexp from matching the first word of a sentence: var wordsToItalicize = ["of", "the"]; for (var w in wordsToItalicize) cod = cod.replace(new RegExp("[color="Red"].[/color]\\b(" + wordsToItalicize[w] + ")\\s\\b", "gi"), "<i>$1</i> "); return cod; Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted March 24, 2016 Share Posted March 24, 2016 This will keep the regexp from matching the first word of a sentence: var wordsToItalicize = ["of", "the"]; for (var w in wordsToItalicize) cod = cod.replace(new RegExp("[color="Red"].[/color]\\b(" + wordsToItalicize[w] + ")\\s\\b", "gi"), "<i>$1</i> "); return cod; It looks like the vBulletin forum "helpfully" converted the   entity in your rule to a regular space, which unfortunately makes the rule not meet the initial requirement of maintaining spaces between words in the output. This is the correct form: var wordsToItalicize = ["of", "the"]; for (var w in wordsToItalicize) cod = cod.replace(new RegExp(".\\b(" + wordsToItalicize[w] + ")\\s\\b", "gi"), "<i>$1</i>&[size="3"]#[/size]32;"); return cod; My trick to make entities such as   to appear correctly here on the forum is to go to Advanced view, select the # character, and under the Sizes drop-down, select 3. Or you can change the size or font or color of any of the characters to keep vBulletin from parsing it as an entity. Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted March 24, 2016 Share Posted March 24, 2016 (edited) Actually, there's another problem with Step's rule. After the first replacement, it will remove the ending semicolon from each previously inserted   entity. FusionPro's parser will still process the entity even though it's malformed, and the output will still be correct, but if you try to apply any other processing to the returned string, things may not work correctly. One way to fix this is to put parentheses around the dot and then use it as another parenthesized substring match in the replacement string, like so: var wordsToItalicize = ["of", "the"]; for (var w in wordsToItalicize) cod = cod.replace(new RegExp("(.)\\b(" + wordsToItalicize[w] + ")\\s\\b", "gi"), "$1<i>$2</i>&[size="2"]#[/size]32;"); return cod; I should add that another option, instead of using   entities, is to use the <space> tag. This tag can optionally have a "count" attribute to specify the number of spaces, or it will default to one if that attribute is not present. So the rule can alternately be written like so: var wordsToItalicize = ["of", "the"]; for (var w in wordsToItalicize) cod = cod.replace(new RegExp("(.)\\b(" + wordsToItalicize[w] + ")\\s\\b", "gi"), "$1<i>$2</i><space>"); return cod; (It was while testing this version with the <space> tag that I noticed the problem of Step's rule stripping out the last character from previous replacements, which in this case would remove the ending ">" from the tag.) By the way, to test this, the first line of my testing rule is: var cod = "The Return of the King"; And I tested it in the Frodo Travel tutorial, which seemed appropriate. Edited March 24, 2016 by Dan Korn 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.