Finding and replacing / transforming text to uppercase by matching using regex

Hopefully someone has tackled something similar in the past? :slight_smile:

  • 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:

  1. ‘WORD WORD UK’ → ‘Word Word UK’
  2. ‘WORD WORD (WORD)’ → ‘Word Word (WORD)’

The steps I have taken so far are:

  1. Transform the whole JSON value for this key to lowercase (:lowercase)
  2. Then capitalise the words (:capitalised words)
  3. Then find and replace Uk with UK

i.e. ‘WORD WORD UK’ → ‘Word Word UK’
So transformation 1 is solved easily :+1:

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”

Hi there, @mat1… this suggestion might not work with everything you’ve got going on there, but if you use the :split by operator, split by an open parenthesis, get the last item in the split, and use the :uppercase operator on that item, you will get the desired result (granted that the result will have a close parenthesis at the end of it, but I’m guessing you can deal with that).

Again, I’m not sure if that will work for you, but I didn’t think it could hurt to throw it out there as food for thought.

Best…
Mike

Thanks @mikeloc - I actually solved this in a very simple way in the end:

  • Use a find and replace
  • Find using a modified regex ([^)]{1,6})
  • Replace using dynamic data referencing the repeating group parent groups JSON body sub-field; extracting the key:value using the same modified regex ([^)]{1,6}) from that value
  • Capitalising the extracted value to complete the replace

Think I needed more coffee this morning :coffee:

Thank you so much for your reply though! I appreciate it!

Mat

This topic was automatically closed after 70 days. New replies are no longer allowed.