Jun 14, 2022 10:08 AM
let old = query[i].field_data;
let new = old.SUBSTITUTE(’\n’,’ \n’);
Results in “SUBSTITUTE is not a function”
Jun 14, 2022 10:28 AM
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.
Jun 14, 2022 11:35 AM
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"
Jun 14, 2022 11:44 AM
If you want a JavaScript global replace you need to use a regular expression with the global flag.
Jun 15, 2022 02:53 PM
Hi,
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…
Jun 15, 2022 04:17 PM
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.