Until some time ago there was only a replace function in list formatting, which allowed us to replace only the first occurrence of a value in a string.
Recently Microsoft released two new operators for list formatting:
- replaceAll
- split
With the replaceAll we can easily replace with all occurrences of a value in a string as in the example below, where we replace client by customer:
replaceAll has 3 parameters: field, original value, replacement value
replaceAll('fullString','value','replacement')
Sample JSON:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "span",
"txtContent": "=replaceAll([$Title],'client','customer')"
}
Result (used in the Updated Text field):
The split operator accepts as a parameter a string and a value to split it:
=split('fullString','valueToSplit')
It outputs an array with the results, so for example, the below expression:
=split('A client trip to meet the client','client')
Would return an array containing the values:
- ‘A ‘
- ‘ trip to meet the’
- ” (empty string)
Based on this, we can use the join operator combined with the split operator to achieve the same result, by joining the split results array based on a word to be the replacement:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "span",
"txtContent": "=join(split([$Title],'client'),'customer')"
}
For a list of all SharePoint list formatting operators check the official documentation:
[…] Source link […]
Is there a way to do multiple replacements within the same string? If I would have a string like:
Apple, Bananas, Potatoes, Peas, Cherries, Beans, Apricots, Plums, Onions, Pears
and would love to replace Potatoes, Peas and Beans?
You could do: replaceAll(replaceAll(replaceAll(‘fullString’,’Potatoes’,’replacement for Potatoes’),’Peas’,’replacement for Peas’),’Beans’,’replacement for Beans’)