Jump to content

Conditional Text Replace


knaselk

Recommended Posts

Hello All,

 

I've run into bit of a problem trying to replace text. I've been using a replace script to add trademarks and register marks to brand names in a product line. For example.:

 

var s = Field("Product Line 1");

FusionPro.Composition.AddVariable

s = s.replace(/Blue/ig, "<span>" + " BLUE" + "<f name=\"Arial\">" + "<p br=false superoffset=180 superratio=25><superscript>®</superscript>" + "</span>");

return s;

 

This works great in most cases, but I've recently come across some variations I'm not sure how to handle. For example, sometimes the product line should be "All BLUE® products on sale", but there are other cases where it should read, "All BLUE Boo Bars® on sale".

 

In the second scenario "BLUE" should not proceeded by a ®. Unfortunately I end up with "All BLUE® Boo Bars®".

 

I have hundreds of brand names to consider and most of them have this sort of variation. Just wondering if anyone has any ideas on how to conquer this. Any help would be greatly appreciated.

Link to comment
Share on other sites

I had something similar to this but not as complex. I just swapped out the letter with the unicode value. It worked in a pinch without having to figure out any complicated regex.

 

s = "All BLUE Boo Bars on sale from Blue.";
s = s.replace(/blue boo/ig, "BLU&\#069; boo").replace(/blue/ig, "BLUE®").replace(/boo bars/ig, "Boo Bars®");
return s;

 

(you don't have to escape the # but the forums were doing their own replace on it back to an E)

 

In your case though, I would probably set this up as a function and use a lookup table for that many replacements.

Link to comment
Share on other sites

Based on the limited number of data points you provide in terms of input data and the corresponding desired output, I'm trying to figure if there's a pattern that can be programmed, and if so, exactly what it is. But I think you just want to leave the data alone if it already as the registered symbol, like so:

var s = Field("Product Line 1");
if (s.indexOf('®') == -1) // if doesn't already contain ®
   s = s.replace(/Blue/ig, "<span>" + " BLUE" + "<f name=\"Arial\">" + "<p br=false superoffset=180 superratio=25><superscript>®</superscript>" + "</span>");
return s;

I took out the line that just had "FusionPro.Composition.AddVariable" on it. That line by itself isn't going to do anything.

 

Also, in a product name such as "BLUE Boo Bars", I'm not sure you're right that both "BLUE" and "Boo Bars" should not be marked as trademarks, or registered trademarks. Presumably, "BLUE" is a company name and "Boo Bars" is the product, both of which may have some kind of trademark applied to them. On the other hand, I also kind of doubt that the word "BLUE" is trademark-able; perhaps you want to apply a service mark or something else to that instead. Anyway, this might be a question for a lawyer-type person.

Link to comment
Share on other sites

Thanks Everyone, I will try this out. Here is a sample data file (Product Line 1 & Product Line 2 are the fields where I'm adding marks):

 

Dan, the client's legal dept. provides me with a very specific list of how to place TM's & ® marks. To be honest, a lot of them don't make sense to me either.

 

I appreciate all of the quick responses, I'll post my progress if it helps anybody else out.

 

Thanks,

Kevin

CA_MAR_2014_Planner_Signage.txt

Link to comment
Share on other sites

Oh, and here is a sample of placement:

 

Beggin’® Collisions (Purina)

Beggin’ Strips® (Purina)

Beggin’® Thick Cut (Purina)

Bil-Jac®

Bio Spot®

Bio Spot Defense® Flea & Tick Spot On®

Blue™

Blue Boo Bars™

Blue Buffalo™

Blue Basics™

Link to comment
Share on other sites

I'm not sure there's an algorithm that can be applied to all of those brand names to figure out where to put all the symbols. It's kind of like the capitalization problem.

 

If it's that important to get all the trademark and registered symbols correct in the output, then they should be included in the input data. Otherwise, well, there's an old saying with computer programs: "Garbage in, garbage out." It's simply not always possible for a computer to calculate things like this, which are based on somewhat arbitrary rules.

 

So I'm not convinced that it's appropriate to rely on FusionPro to repair flawed or incomplete input data like this. But if you really want to do it, I think that Thomas is correct: You will need to make a mapping of raw data to "decorated" output strings, with one entry for each brand, something like this:

var symbolReplacements =
{
   "Beggin’ Collisions (Purina)": "Beggin’® Collisions (Purina)",
   "Beggin’ Strips (Purina)": "Beggin’ Strips® (Purina)",                          
   "Beggin’ Thick Cut (Purina)": "Beggin’® Thick Cut (Purina)",
   "Bil-Jac": "Bil-Jac®",
   "Bio Spot": "Bio Spot®",
   "Bio Spot Defense Flea & Tick Spot On": "Bio Spot Defense® Flea & Tick Spot On®",
   "Blue": "Blue™",
   "Blue Boo Bars": "Blue Boo Bars™",
   "Blue Buffalo": "Blue Buffalo™",
   "Blue Basics": "Blue Basics™",
   // etc.
}

var rawValue = Field("Product Line 1");
return symbolReplacements[rawValue] || rawValue;

Link to comment
Share on other sites

Thanks, Dan. I totally agree, it's not easy getting good data. What I've done in the past is try to catch all of the major marks with a rule, but if one is missed I have the client add a "-R" or "-TM" to the input file. We go through several rounds of revisions so it's not a huge deal to have it 100% correct the first time.

 

Thomas, your bit of script is works well! I think it will ultimately be worth my time to program this. I can use it across multiple templates as well.

 

Again, thanks for all the help!

Link to comment
Share on other sites

UPDATE: Not the most elegant, but this seems to be working well. Basically just removing the TM when the other keywords are found :

 

s = Field("Product Line 1");

s = s.replace(/blue/ig, "BLUETM").replace(/bluetm boo bars/ig, "BLUE Boo BarsTM").replace(/bluetm buffalo/ig, "BLUE BuffaloTM").replace(/bluetm basics/ig, "BLUE BasicsTM");

return s;

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