Jump to content

How to write a multiple "find and replace" code


rpaterick

Recommended Posts

I'm trying to find certain words in the data and replace them during the run.

 

Right now I have this for a code but can't understand what's what.

 

return Field("Full Name").replace(/[\<\>\/]/g," ");

 

 

I'd like to replace SR to Sr and Ii to II or Iii to III.

 

Any ideas?

 

Thanks

Link to comment
Share on other sites

I'm trying to find certain words in the data and replace them during the run.

 

Right now I have this for a code but can't understand what's what.

 

return Field("Full Name").replace(/[\<\>\/]/g," ");

Well, you have a function that basically replaces angle brackets and slashes with spaces. That's an interesting function, but it doesn't seem very relevant to what you're trying to do:

I'd like to replace SR to Sr and Ii to II or Iii to III.

You don't really need a regular expression to do this; you can simply call the ReplaceSubstring function as many times as you need in a single rule. For example:

var s = Field("Full Name");
s = ReplaceSubstring(s, "SR", "Sr");
s = ReplaceSubstring(s, "Iii", "III");
s = ReplaceSubstring(s, "Ii", "II");
return s;

But, you can definitely use regular expressions, which can cover more possibilities of things to replace, and you can chain calls to String.replace together. Try this:

return Field("Full Name").replace(/\b[js]r\b/gi, function(w){return ToTitleCase(w);}).replace(/\bi+\b/gi, function(w){return ToUpper(w);});

That will replace all instances of "JR" or "SR", in any case, but only as entire words, and also all instances of more than one "I" in any case, as an entire word.

Link to comment
Share on other sites

Try this:

return Field("Full Name").replace(/\b[js]r\b/gi, function(w){return ToTitleCase(w);}).replace(/\bi+\b/gi, function(w){return ToUpper(w);});

That will replace all instances of "JR" or "SR", in any case, but only as entire words, and also all instances of more than one "I" in any case, as an entire word.

 

Thanks Dan for your help. I just posted a new issue with an external data file that I hope you can help also.:D

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...