Jump to content

Using two Replace Functions in one field


-Lisa-

Recommended Posts

I've got the first half of this rule working, but I can't seem to figure out the second and was hoping for some guidance.

 

I need to replace the registered trademark in a product name and then follow that up with italicizing the second half of the product name. So far I have:

 

return Field("ProductDescription").replace(/DENTtips Master/g, "DENTtips" + '<f name="Compatil Fact It"> Master</f>);

 

The registered mark needs to come right after "DENTtips" and before "Master". However, because the word "master" may be used in another grammatical context, I cannot force a global replace around "master" By itself. I tried the following which didn't work:

 

return Field ("ProductDescription").replace(/DENTtips® Motion | DENTtips® Motion/g, "DENTtips + '<superscript>®</superscript>' + '<f name=Compatil Fact It"> Master</f>);

 

I know I'm missing something easy here!!

Link to comment
Share on other sites

Lisa, if you're trying to capture separate patterns, put them inside parentheses like so:

return Field ("ProductDescription").replace(/[color="Red"]([/color]DENTtips® Master[color="red"])[/color]|[color="red"]([/color]DENTtips® Master[color="red"])[/color]/g,'DENTtips<superscript>®</superscript><f name="Compatil Fact It"> Master</f>');

 

Also, I'm not sure if it's on purpose or not, but your code is searching for "Motion" and in your post you say you're trying to replace "Master" so I made that edit.

Link to comment
Share on other sites

Thanks Step. I appreciate the reply.

 

Unfortunately, that doesn't seem to return the result I need. In fact, neither format style is being applied. I don't see the registration mark super-scripted nor is the word "Master" italicized the way it should be.

 

(BTW - thanks for changing that in my code. I've got two different products I need to replace, one is Master the other, Motion. Looks like I copied the wrong one into the thread!)

Link to comment
Share on other sites

Try this:

return Field("ProductDescription").replace(/(DENTtips) (Master)/g, '$1<superscript>®</superscript>&[size="2"]#[/size]32;<f name="Compatil Fact It">$2</f>');

To break this down: the $1 and $2 in the second parameter call out the first and second "captured" substrings, denoted by parentheses in the Regular Expression in the first parameter. So those always return "DENTtips" and "Master". The rest is just markup: the &#32; is there to ensure that the space is typeset (otherwise a space between two tags would be collapsed by FusionPro's tagged markup parser). Also note the quotes in the <f> tag, which are needed as the "name" attribute value has spaces.

I've got two different products I need to replace, one is Master the other, Motion.

You can do that with a tweak to the Regular Expression:

return  Field("ProductDescription").replace(/(DENTtips) (Master|Motion)/g, '$1<superscript>®</superscript>&[size="2"]#[/size]32;<f name="Compatil Fact It">$2</f>');

More info:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Link to comment
Share on other sites

Try this:

return Field("ProductDescription").replace(/(DENTtips) (Master)/g, '$1<superscript>®</superscript>&[size="2"]#[/size]32;<f name="Compatil Fact It">$2</f>');

To break this down: the $1 and $2 in the second parameter call out the first and second "captured" substrings, denoted by parentheses in the Regular Expression in the first parameter. So those always return "DENTtips" and "Master". The rest is just markup: the &#32; is there to ensure that the space is typeset (otherwise a space between two tags would be collapsed by FusionPro's tagged markup parser). Also note the quotes in the <f> tag, which are needed as the "name" attribute value has spaces.

 

Thanks, Dan! This was super helpful. And I really appreciate the additional explanation and the links!

 

My only concern is that there are only limited instances where the registered trademark is required. This template will be used in conjunction with a MarcomCentral storefront. The users know when the registered trademark is and is not required. Therefore I need to account for both scenarios.

 

For instance, using "Motion" as the example here, if someone enters "DENTtips® Motion", the end result should superscript the registered mark and italicize the word "Motion". However if the mark is not entered, and only "DENTtips Motion" is returned, I still need "Motion" to be italicized.

 

I tried altering your code to:

return Field("ProductDescription").replace(/(DENTtips) (®) (Master)/g, '$1$2<superscript>®</superscript> <f name="Compatil Fact It">$3</f>')

 

But that doesn't seem to give me the desired result...

Link to comment
Share on other sites

Thanks, Dan! This was super helpful. And I really appreciate the additional explanation and the links!

 

My only concern is that there are only limited instances where the registered trademark is required. This template will be used in conjunction with a MarcomCentral storefront. The users know when the registered trademark is and is not required. Therefore I need to account for both scenarios.

 

For instance, using "Motion" as the example here, if someone enters "DENTtips® Motion", the end result should superscript the registered mark and italicize the word "Motion". However if the mark is not entered, and only "DENTtips Motion" is returned, I still need "Motion" to be italicized.

 

I tried altering your code to:

return Field("ProductDescription").replace(/(DENTtips) (®) (Master)/g, '$1$2<superscript>®</superscript> <f name="Compatil Fact It">$3</f>')

 

But that doesn't seem to give me the desired result...

This gets a bit more complicated to do with a String.replace call and a Regular Expression, but I think this will work:

return TaggedDataField("ProductDescription").replace(/(DENTtips)(®|) (Master|Motion)/g,
   function(all, m1, m2, m3){return m1 + '<superscript>' + (m2 || '®') + '</superscript>&[size="2"]#[/size]32;<f name="Compatil Fact It">' + m3 + '</f>';});

Here, instead of a string with replacement patterns such as $1, $2, etc., we're specifying a function which takes those matched substrings as parameters. We make the second match optional by saying basically "or nothing" in the RegExp, and then we handle that in the function by saying "or '®'". Note also that, since we added another set of capturing parentheses, the "Master|Motion" part is now the third match. I've also changed the Field call to TaggedDataField so that, if the user does enter a literal ® symbol, it gets converted to the entity (and so that other things which need escaping in tagged markup get handled properly).

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