Substituting string values
Sometimes, you need to substitute one string value for another. Perhaps data was added to the data source inconsistently. For example, some addresses contain “Street,” and some contain “St.”. You can replace entire string values or just parts of a string by using the replace( ) function in JavaScript.
The replace( ) function searches for a specified string and replaces it with another string. It takes two arguments: the string to replace, and the new string. The following expression searches for “St.” in an address field and replaces it with “Street”:
row["address"].replace("St.", "Street")
To search for and replace multiple strings in a single field, add as many replace( ) functions as needed to the expression, as shown in the following example:
row["address"].replace("St.", "Street").replace("Ave.", "Avenue").replace("Blvd", "Boulevard")
As with any global search-and-replace operation, be aware of unintended string replacements. For example, the row["address"].replace("St.", "Street") expression replaces St. Mary Road with Street Mary Road. In this case, rather than just searching for “St.”, you need to search for “St.” at the end of a line. To perform this type of search, specify a string pattern to search, rather than a literal string. For more information about searching for patterns, see
“Matching string patterns,” later in this section.
To replace entire strings, rather than just a part of the string, you can use the mapping feature instead. The mapping feature is ideal for replacing known sets of values. For example, a gender field contains two values, M or F. You can map the M value to Male, and F to Female. For more information about mapping values, see Specifying alternate values for display.