Problem with replace formatter on Node

I also want to strip dots and commas from a $POST input value (string) and then convert it to a number and also need to edit the expression manually to work around the Field is required alert.

This expression indeed strips the commas from the string and converts it to a string:
$_POST.value.replace(',', '').toNumber()

This also works as expected:
$_POST.value.replace(’.’, ‘’).toNumber()

But the combination always returns 0:
$_POST.value.replace(',', '').replace('.', '').toNumber()

So, “How to strip multiple characters/words from a string” in 2021?

Solved it using a regular expression:

$_POST.value.replace(/[.,]/g, '').toNumber()

Hi

Could you please explain me your working expression ?
It is different from what I always see with formatterà.

Thank you
roberto

Hi roberto,

This part is a regular expression, that’s why it looks so different.

I’m puzzled about how you did this too and would interested in more details.

While you can user regular expressions with the Replace function in App Connect, I thought it wasn’t possible with Server Connect. In my experience it’s not possible (strangely, except for validation), and I thought this had also been confirmed in a one or two threads, like this one.

It works in SC too:

image

You have to type the code manually, there’s no way to pick it from the data formatting menu, it actually shows it like this there:

image

You guys are most probably working with different server models.
@Emiel there was an issue with replace for nodejs indeed, so replacing characters like , or . was not working correctly.
This will be fixed in tomorrow’s update, so your expression

your_value.replace(',', '').replace('.', '').toNumber()

will work just fine and you won’t have to deal with regexp.

1 Like

I was just thinking this might be the case. I’m on node js indeed.

Great there’s a fix for the replace function in tomorrow’s update. However, now I set it up this way, is there a downside to it or can I just as well leave it as it is now?

No, all is fine. I was just thinking it will be easier to deal with this using the existing formatters.

1 Like

Yes, I was using PHP. So does regex work with NodeJS in Wappler (if the expressions are entered manually)?

Regex is difficult - but it can also make things a lot easier and simpler.

Okay cool, thanks @Teodor :+1:

This has been fixed in Wappler 3.9.0

It would be useful to know - thanks.

This topic was automatically closed after 29 hours. New replies are no longer allowed.