Hopefully someone has tackled something similar in the past?
- I have a JSON response from an API
- The key: value want to display in my app is returned in all caps
- I want to carry out transformations before displaying on the page and subsequently saving to the database
I need two transformations to be carried out:
- ‘WORD WORD UK’ → ‘Word Word UK’
- ‘WORD WORD (WORD)’ → ‘Word Word (WORD)’
The steps I have taken so far are:
- Transform the whole JSON value for this key to lowercase (:lowercase)
- Then capitalise the words (:capitalised words)
- Then find and replace Uk with UK
i.e. ‘WORD WORD UK’ → ‘Word Word UK’
So transformation 1 is solved easily
But I am then stuck because bubble doesn’t support find and replace using regular expression and then manipulating this in the front end.
So for transformation 2 I want to match:
- /(([^)]+))/g i.e. everything within brackets is matched
- and then manipulate this so that everything within () is transformed to uppercase.
The question is how can I achieve transformation 2?
Explained differently:
JS:
const str = “This Is A Test (Test)”;
const transformedStr = str.replace(/(([^)]+))/g, function(match, p1) {
return “(” + p1.toUpperCase() + “)”;
});console.log(transformedStr);
Returning:
This Is A Test (TEST).
In totality I want to achieve the following for an given isolated value/string:
function transformString(str) {
// Transform the entire string to lowercase
str = str.toLowerCase();// Capitalize each word
str = str.replace(/\b\w/g, function(char) {
return char.toUpperCase();
});// Transform anything within parentheses to uppercase
str = str.replace(/(([^)]+))/g, function(match, p1) {
return “(” + p1.toUpperCase() + “)”;
});// Define an array of isolated strings to transform to uppercase
const isolatedStrings = [‘uk’];// Transform isolated strings to uppercase
isolatedStrings.forEach(function(isolatedString) {
const regex = new RegExp(\\b${isolatedString}\\b
, ‘gi’);
str = str.replace(regex, isolatedString.toUpperCase());
});return str;
}// Test the function
console.log(transformString(“hello world (test) uk”)); // Output: “Hello World (TEST) UK”