I must replace some special characters on Code by Zapier - javascript

I need to replace some special characters on Zapier.
Since there are many substitutions, I want to do it through Code by Zapier.
I made the code, but I got the following error on Code by Zapier:
We had trouble sending your test through.
TypeError: str.replace is not a function
Here is the code:
str = inputData;
str.replace(/Á|Ä|À|Ã|Â/g, "A");
str.replace(/É|Ë|È|Ê/g, "E");
str.replace(/Í|Ï|Ì|Î/g, "I");
str.replace(/Ó|Ö|Ò|Õ|Ô/g, "O");
str.replace(/Ú|Ü|Ù|Û/g, "U");
output = [{outputData: str}];
I don't know why str.replace doesn't work on Code by Zapier. I tried use str = str.replace(), instead str.replace(), but it did not worked too.
Can someone help?

Looks like inputData is not a string but rather an object. So if you provided a field to the Code then you need to access it as a property of that object. Let's say the name of the variable is foo, then you need to access it as inputData.foo.replace() (see screenshot).

Related

How to use `%value%` in javascript?

I am learning JavaScript and I see %value% in a code but I do not know what does it mean or how to use it. Can anyone please help me explain to me. Thank you very much.
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
"%data%" is just a literal string. This code will take the value of HTMLWorkLocation, look for the first occurrence of %data% in it, and replace that with the value of work.jobs[job].location, and store the resulting string in formattedLocation.
var work = {
jobs: [{
location: "Home office"
}]
};
var job = 0;
var HTMLworkLocation = "John is located at %data%";
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
console.log(formattedLocation);
This is probably part of a template system that's used to replace placeholders like %data% with values that come from a table.
You're using string.replace which takes a string or regular expression as it's first argument. Based on the code you posted it looks like you're looking for the string "%data%" (or whatever string you're looking for) in HTMLworkLocation and replacing it with the value in work.jobs[job].location. Then it is being stored in formattedLocation.
I would put a debugger; line after that line of code so you can see what the values are in the debugger console. That might help make more sense of things.
Here is more info on the str.replace method with some examples

javascript json parse with single quote

I am trying to transform into a json object the an object this works well with the following object and the associated code :
myStr = "{'key':'value'}"
JSON.parse(myStr.replace(/'/g, "\""));
The problem is that with the same code, I am getting an exception with the following object :
myStr = "{'key':'val'ue'}"
The error is unexpected token u. I would like to keep the single quote between l and u, and not replace it.
Is there anyway?
You could do this, but it feels a little hacky...
JSON.parse(myStr.replace(/{'/g, "{\"").replace(/'}/g, "\"}").replace(/':'/g, "\":\""));
Hope this may help you. Assuming the inner singe quote comes between alphabets.
myStr = "{'key':'val'ue'}"
JSON.parse(myStr.replace(/([a-z])'([a-z])/g, "$1\\'$2"));

substr in IE7 not working

I'm trying the following code to extract first few characters from a string stored in a variable.
title.substr(0,35)+"_"+var1+"_"+var2+var3;
At the above line IE7 throws an error:
Object doesn't support this property or method.
Tried: substring and slice as well. Still same issue.
It is quite apparent from the error message that title is not of type string.
Try this:
var title = "abcdefghijklmnopqrstuvwxyz";
console.log(title.substr(0,15));
I suspect your title variable is not of type String. To debug it, try
console.log(typeof(title)) in your own code.
Check the console and see what you get.

Javascript regex match doesn't run correctly

okay I give up. Here's my code:
var re = /href="(http.*\.jpg)"/g;
var mp3s = body.match(re);
it finds pictures, but it returns href="http://www.picture.com/smthg.jpg"
instead of returning http://www.picture.com/smthg.jpg
any idea why?
The result from match() is actually an object.
I think you need to access the first element on that object.
For example:
body.match(re)[1]
This is where the actual result is kept.
Shameless self-promotion:
I've written a small guide for me, I can never remember how to use these either. It's here: http://queirozf.com/reminders/javascript-regular-expressions-usage-reminder
try
var re = /(http.*\.jpg)/g;
var mp3s = body.match(re);
since you don't need the href.
You want to match the regular expression, but then return just the portion in brackets.
To do this, call the regular expressions exec method. For example:
var body = 'stuff stuff morestuff href="http://www.picture.com/smthg.jpg" and some more stuff';
var re = /href="(http.*\.jpg)"/g;
var regexResults = re.exec(body);
var mp3s = regexResults[1];
alert(mp3s);
Having given you this answer, I must implore you to find a different way to solve this problem. You cannot parse HTML using regular expressions. No matter how sophisticated your regular expression gets, there will be a legal HTML example which will break it.

unicode to utf-8 in JavaScript

i have output from a server like
["alex", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"]
i want to convert it like
["alex", "to its right language"]
using js or jquery
i tried
function encode_utf8( s )
{
return unescape( encodeURIComponent( s ) )
}
but not working correctly
any help?
thanks in advance
I am not sure about what you mean by getting output "LIKE" the shown example...
but if you get ["alex", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"] and assign it to a variable like
var foo = ["alex", "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"];
// alert(foo[1]) results in "��������" which actually means
// the engine has at least tried to resolve the characters
for example if you pass in correct character codes like:
var foo = ["alex", "\u003cp\u003emy UTF paragraph\u003c/p\u003e"];
// alert(foo[1]) results in "<p>my UTF paragraph</p>" which seems correct to me...
Try the examples above in a browser console (works at least for me in current Chrome)
On the other hand if you receive "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" every time then I assume similar to the commentors that your response already gets messed up before you are handling it in JavaScript
This article shows nicely that using Unicode characters is valid for variable naming so the same should apply to string content.

Categories