SUBSTITUTE is not a function

let old = query[i].field_data;
let new = old.SUBSTITUTE(’\n’,’ \n’);

Results in “SUBSTITUTE is not a function”

It looks like you are writing a script versus writing an Airtable formula. Scripts are written in JavaScript, formula fields are written in Airtable’s formula field language. SUBSTITUTE is a function in the Airtable formula field language. It is not a function in the JavaScript language. You might want the JavaScript string function .replace().

You also need to make sure that you are using .replace() on the proper data type. It also seems like you are trying to replace a newline character with another newline character, which wouldn’t have any effect.

let old = query[i].field_data;
let new = old.replace(’\n’,’

replace() does work but its working for the first occurrence only. I have a following string,

"One \n - Two \n - Three"

need this as,

"One </br> - Two </br> - Three"

If you want a JavaScript global replace you need to use a regular expression with the global flag.

for those (like me) too lazy to learn regex for now, you can use split-join.
example, used in html txt parsing to get text from tag A to tag Z excluding all occurrences of tag X inside.

const cutter=(txt,pattern)=>txt.split(pattern).join(’’);
const cut=(txt,a,z,x)=>cutter((txt.split(a,2).pop().split(z,2).shift()), x);

but anyway, i’m going to start learning regex one day…

You can use the string .replace method with minimal to no regex knowledge. The webpage I linked to has an example of a global replace with static strings.

1 Like

This topic was solved and automatically closed 15 days after the last reply. New replies are no longer allowed.