Google Scripts extracting a specific ID - javascript

I'm trying to figure out how to pull a unique identifier from a Gmail message. I've got the body pulled with:
function myFunction() {
var emails = GmailApp.search('label:company-email subject:"Specific Criteria" ',1,10);
for (x=0;x<emails.length;x++){
var thread = emails[x].getMessages()[0].getPlainBody();
var UID; // need this varible to be set as the extracted UID formatted UID### with 3 possibly 4 numbers. It will always have the same starting "UID"
}
}
UID: need this varible to be set as the extracted UID formatted UID### with 3 possibly 4 numbers. It will always have the same starting "UID"
Edit:
I have researched regex to see if I could get the data with a regular experession, however i'm in over my head there and I'm not understanding it.
I also tried indexOf(), but when I search the thread it gives me a -1. I tested to make sure I had indexOf() correct by testing it with a phrase and the UID, and it pulls when I have a regular string, just not from the body of the email. Maybe I'm missing a conversion somewhere?
Edit #2:
Here is a sample of the email being received:
Good afternoon user,
Please contact *Company Name* (UID123) (*City,State*) (*additional info*) at your earliest convenience.
Thank you,
I deleted the regex and am gonna be honest I don't remember exactly what I tested. was doing that late last night.
Edit 3:
turned out I was doing something wrong with regex. got an answer and I needed the following:
var UID = thread.match("UID[0-9]+"); worked like a charm.
Thank you guys for helping.

Use string#match
\d+ - one or more digits
const msg = `Good afternoon user,
Please contact *Company Name* (UID123) (*City,State*) (*additional info*) at your earliest convenience.
Thank you,`;
console.log(msg.match(/UID(\d+)/)[1])

Related

Special characters on input come in as %nn

I have an HTML FORM requesting the name of a movie to search for, and a JavaScript function which takes that name passed as a query parameter (after a ?) on a reinvocation of the page URL and compares it against a database (read with AJAX). The problem is that special characters are being translated on input to %nn, so they don't match the names in the database. For example, when I read the user input of "airplane!" with "title = window.location.search" I receive "airplane%21". How can I defeat or correct this behavior, or am I going to have to translate every %nn in my JavaScript routine? Thank you.
(Page reference: https://acb.org/adp/findavideo.html)
Arggh! Not 2 seconds after I posted this, someone sent me an answer offline: "title = decodeURIComponent(title)" does the trick. Hope this helps someone else.
You can try decode the value before use it, for example:
decodeURIComponent('%21'); // "!"

Using JavaScript and Regex to extract 1 specific data from URL

I would like to get from an URL like these JUST the "test#gmail.com" (or whatever email will be in the URL) :
www.website.com/?email=test#gmail.com
www.website.com/?email=test#gmail.com&name=john
www.website.com/?name=john&email=test#gmail.com
My problem is to solve that all of these 3 cases can happen (so no other data, OR data after the email, OR data before the email).
Unfortunately I have zero Regex-Skills, but was able to get so far together these 2 things:
/email=(.*)/
/email=(.*)\&/
The 1st one works if the email is the ONLY data.
The 2nd one works just if there is another data after my email.
But as mentioned I need to make it work regardless of which of the 3 types above is the case.
Could you help me out please?
Thank you very much!
BTW, I need it for this:
var msg = window.location.href.match(/email=(.*)/);
(I'm searching for an answer for more than 2 hours, used Google, used Regex-Testers, checked out Cheat Sheets and read many StackOverFlow-Questions... But I can't solve it on my own.)
Try this regex: https://regex101.com/r/gMyqDa/1
It matches email patterns not just any characters after email=
if your cases are that simple, then just exclude the ampersand
var urls = [
"www.website.com/?email=test#gmail.com",
"www.website.com/?email=test#gmail.com&name=john",
"www.website.com/?name=john&email=test#gmail.com"
];
for (i in urls) {
var url = urls[i];
console.log(url);
console.log(url.match(/email=[^&]+/)[0]);
}

SyntaxError: identifier starts immediately after numeric literal?

I have been working on my project where I had to do some updates on my data records. After I finished my update I got an error: SyntaxError: identifier starts immediately after numeric literal and this line of code was below that error in firebug : maxScores.ew-19a = ''
I looked up in my code and I found where is this output coming from, here is the code:
var maxScores = new Object;
<cfoutput query="getRec">maxScores.#LCase(tCode)# = '#maxScore#';</cfoutput>
In my update I had to put - symbol between letter and number, in old data I did not have that so I think that causing the problem here. I was wondering how I can prevent this or if there is any method that I have to put around my output to prevent this? If you know how this can be fixed pleas let me know. Thank you.
ColdFusion will attempt to subtract ew from 19a when you just dump it between to pound signs like that. You will need to use bracket/object notation here. Try this:
<cfoutput query="getRec">
maxScores.#LCase(getRec["tCode"][currentrow])# = '#getRec["maxScore"][currentrow]#';
</cfoutput>
If you want to Lower case something do it in the query. Outputing JS using CF is useful and solves many problems, but you want to keep it as clean as possible to keep your brain from fogging over. :)

Java Script and RegEx

In application that I'am working on I have dynamically generated UI it is number of controls and listed entries depends on records from DB.What I'am trying to achieve is to write RegEx that would help me extract some numbers assigned as part of controls ID.
The way how I'am assigning IDs is : cText+(row that it represents in its group)+G+(group number).For example cText4G12
What I'am looking for is to get from that ID what Row it represents in example is 4 and it's group 12. I've never used RegEx before so I'am asking for your help :)
RegEx is not something that is easy to get your head around, so I would strongly recommend that you spend a bit of time to learn it - as it will be beneficial to you in the long run.
However, against my best judgement - here is code that will get you what you need..
var id = "cText4G12";
var result = id.match(/^cText(\d+)G(\d+)$/);
result[1] will contain "4"
result[2] will contain "12"
You don't really need regex. You can simply do:
var my_id = "cText4G12";
var row = my_id.slice(my_id.indexOf("cText")+5,my_id.indexOf("G"));
var group = my_id.slice(my_id.indexOf("G")+1);

Adding fraction with integers in JavaScript

Here is what my friend noticed while working on a project. He does not have any stackoverflow account so I am asking on behalf of my friend.
var a = 1.75/3;
so it gives
a = 0.5833333333333334
Now when I add 1 to variable a I get this:
1.5833333333333335
Notice the difference in the last digits
Similarly when I do following
0.5833333333333336+1
I get
1.5833333333333335
Now replacing the 6 with 7
it gives me
1.5833333333333337
I can't understand what is going here. Can anyone please explain this?

Categories