Jump to content

need to keep a space between words


blyons

Recommended Posts

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!

Link to comment
Share on other sites

You need to replace the spaces after the tags with &#32; 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 by Dan Korn
Added \b word boundaries to not capture partial words.
Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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 &#32; 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 &#32; 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.

Link to comment
Share on other sites

Actually, there's another problem with Step's rule. After the first replacement, it will remove the ending semicolon from each previously inserted &#32; 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 &#32; 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. :cool:

Edited by Dan Korn
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.

Guest
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...