I have the ability to include basic Javascript functions in a web-app (FormAssembly) and I need it to parse and do some string replacement. I don't know JS and have been struggling to figure out how to get the proper syntax and logic, so hopefully this makes sense.
An example of an allowed Javascript function in this app is: if(var=="banana"){"banana;"}else{""}
The input text var can be in the form of a few fruits, just as banana, bananagrape, or bananagrapekiwi. There are up to 5 values, and I know all the possible values. Basically I want to append a semicolon ; character to all the fruits so that the output text would be banana; or banana;grape;kiwi;
Here's what I've come up with. I'm not able to retrieve error message from this web app FormAssembly so I can't tell what's not working:
<script>
const parseFruit = (val) => {
return val.replace(/banana/g,"banana;").replace(/kiwi/g,"kiwi;")
}
</script>
Any help to get the proper syntax would be appreciated
Update: looks like the initial function is working for input parameters that don't have any spaces. How would I include spaces in the first parameter in the replace function?
<script>
const parseFruit = (val) => {
return val.replace(/banana/g,"banana;").replace(/other fruit/g,"other fruit;")
}
</script>
if(var=="banana"){"banana;"}else{""}?errorif there's no input at first. Updating the question to include another scenario of spaces in the input.valcould beundefined, that would explain the error. Try surrounding withif (val === undefined) { return ""; } else { return val.replace(...) }