/    Sign up×
Community /Pin to ProfileBookmark

Find String Set Variable

Hello,
I have been put in charge of crm system that uses jscript to parse incoming emails for information. I wasnt involved to much in the setup and i have come a bit stuck with one part.

The below code is supposed to pull out a refernence ID for emails that need them in the reply so it is routed correctly however one of the senders changed the format of emails to HTML only so i have had to convert the incomming emails to plain text like it used to however they have also changed the format of a part of text as well. Esentially in the converted email i get this in the body somewhere:

[B]Email reference ID:
[#a03-sveziizjlf#]_[#2e5bf907ba03444bbc675267617dc1b2#]
[/B]

It used to be next to each other so there was only a space between the two sections like so [B]Email reference ID: [#a03-sveziizjlf#]_[#2e5bf907ba03444bbc675267617dc1b2#]
[/B]

The below code worked for the single line format but not for the one with the new line. Could anyone sugest how i can change it to pull out the string for the ID ([#a03-sveziizjlf#]_[#2e5bf907ba03444bbc675267617dc1b2#]) OR could somone suggest a way i can get it working so that it finds the first [# then keeps going till the last #] this would be better i think as i knwo this wont change.

[CODE]vEMst = EMBODY.indexOf(“Email reference ID: “);
if (vEMst != -1)
{
vEMst = vEMst + 19
vEMTest = EMBODY.substring(vEMst, vEMst + 1);
if (vEMTest != “[“)
{
vEMTest = vEMTest + 1;
}
vEMen = EMBODY.indexOf(“]<“, vEMst);
if (vEMen != -1)
{
EbayEmailfdets = EMBODY.substring(vEMst, vEMen + 1);
}

}[/CODE]

Many thanks,

Tom

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 15.2015 — Try to remove the newlines in your string:
[CODE] str = EMBODY.replace(/n/, "");
re = /.*Email reference ID: ([#.*#]_[#.*#]).*/;
matches = re.exec(str);
EbayEmailfdets = matches[1];[/CODE]
Copy linkTweet thisAlerts:
@tomaskauthorSep 16.2015 — Thanks for your reply however i am unsure where to place this in the overall function (sorry i'm not totally up to speed with jscript) Below is the full function for this part of the whole script:


[CODE]
function getEmailRef(EMBODY)
{

EbayEmailfdets = "";
vEMst = EMBODY.indexOf("Email reference id: ");
if (vEMst != -1)
{
vEMst = vEMst + 19
vEMTest = EMBODY.substring(vEMst, vEMst + 1);
if (vEMTest != "[")
{
vEMTest = vEMTest + 1;
}
vEMen = EMBODY.indexOf("]<", vEMst);
if (vEMen != -1)
{
EbayEmailfdets = EMBODY.substring(vEMst, vEMen + 1);
}
}
vEMst = EMBODY.indexOf("Email reference ID: ");
if (vEMst != -1)
{
vEMst = vEMst + 19
vEMTest = EMBODY.substring(vEMst, vEMst + 1);
if (vEMTest != "[")
{
vEMTest = vEMTest + 1;
}
vEMen = EMBODY.indexOf("]<", vEMst);
if (vEMen != -1)
{
EbayEmailfdets = EMBODY.substring(vEMst, vEMen + 1);
}

}
vEMst = EMBODY.indexOf("Email reference ID:");
if (vEMst != -1)
{
vEMst = vEMst + 19
vEMTest = EMBODY.substring(vEMst, vEMst + 1);
if (vEMTest != "[")
{
vEMTest = vEMTest + 1;
}
vEMen = EMBODY.indexOf("]<", vEMst);
if (vEMen != -1)
{
EbayEmailfdets = EMBODY.substring(vEMst, vEMen + 1);
}

}
MsgHandler.Log("Function Ebay mail ID = " + EbayEmailfdets);
} [/CODE]
Copy linkTweet thisAlerts:
@SempervivumSep 16.2015 — This replaces your complete code:
[CODE] str = EMBODY.replace(/n/, "");
re = /.*Email reference [Ii][Dd]: *([#.*#]_[#.*#]).*/;
matches = re.exec(str);
EbayEmailfdets = matches[1];[/CODE]
You can test it here and try different strings:

http://jsfiddle.net/Sempervivum/y7zfraxt/

(Change variable str and press "Run").
Copy linkTweet thisAlerts:
@tomaskauthorSep 16.2015 — Sempervivum,

Thank you very much for this i suspected it was a replacement just wanted to be sure. Its done the trick in fact its even working on some of the other queries i didnt think it would! ?

Having studied it a bit i understand how its working as well going to be going through the rest of the script to make sure i understand the whole thing as i can see some improvemetns i could make as some of the functions are very long for what they are doing.

Once again many thanks,

Tom
Copy linkTweet thisAlerts:
@davejjjSep 16.2015 — I would be a little reluctant to mess with a regx to simply remove whitespace between "Email reference ID: " and the next "[" since a simple little while-loop could step ahead to find the "[" with very little overhead.
×

Success!

Help @tomask spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...