Simultaneous String Argument combining slicing and and REGex

Explorer ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

The following script involves two fields: 'OldbusinessName' and 'NewbusinessName' ; and is placed in the custom calc of field 'NewbusinessName'. I'm attempting to examine the user-entered value from field 'OldbusinessName', to identify whether the word "The" is used in the first part of the name and if it is, remove it (ex. 'The Hertz Company' would become 'HertzCompany') . Secondly, if numbers are used in the first part of the name and if so, remove them (ex. '911 - STOP SHOP' would become 'STOPSHOP'). Third, these two events must occur similtaneously (ex. 'The 911 - STOPSHOP' would become 'STOPSHOP') and present as an intermediate value from which the first letter is only displayed as the ultimate calculated value in the 'NewbusinessName' field. I can't seem to figure out how to combine these tasks successively, only individually. Any guidance is greatly appreciated- thanks in advance!

I attached the file for reference.


//if begins with number, remove beginning numbers and extract first letter and assign it to a variable.
//if begins with "the","The","tHe","thE","THe","THE","tHE" followed by a space, slice //"the","The","tHe","thE","THe","THE","tHE" and empty space after and assign it to a variable


var OLDNAME = this.getField("OldbusinessName").value;
var firstChar = OLDNAME.charAt(0);
var secondChar = OLDNAME.charAt(1);
var thirdChar = OLDNAME.charAt(2);
var fourthChar = OLDNAME.charAt(3);


if (OLDNAME == "") {event.value = "";}
else if (firstChar = "T"||"t") && (secondChar = "H"||"h") && (thirdChar = "E"||"e") && (fourthChar = "")) {event.value.slice(5);}
else if {
var NEWNAME = OLDNAME.replace(/[^A-Za-z]+/g, '');
var firstChar = NEWNAME.charAt(0);
event.value = firstChar; }

TOPICS
How to, JavaScript

Views

194

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct Answers

Adobe Community Professional , Mar 14, 2021 Mar 14, 2021
This does exactly what you asked for:  event.value = this.getField("OldbusinessName").valueAsString.replace(/^the/i,"").replace(/\d+/g,""); However, there are a couple of complications you failed to mention in your explaination but included in the example, i.e. the removal of puctuation and white space around the numbers and the word "the".  To write a successful regular expression you must be explicit and precise in the description of what you want to do. Read this: https://www.pdfscripting.com/public/Pattern-Matching-with-Regular-Expressions.cfm...

Likes

Translate

Translate
Adobe Community Professional , Mar 14, 2021 Mar 14, 2021
The code operates correctly. It does exactly what it is supposed to do as it is written. I think you need to go back to my first comment about being explict and precise, and the additional complication. Both the word "the" and the numbers are replaced with empty strings, so the value in the "OLDNAME" variable is "  stop". Note the first two characters are spaces.  So, the value of the calculation is a single space.  In programming you get exactly what you code, nothing more, nothing less. If y...

Likes

Translate

Translate
Most Valuable Participant ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

To check if the string begins with "the", in any configuration of lower-/upper-case letters, you can use this regular expression:

 

if (/^the/i.test(OLDNAME)) {

// do something

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

I had it separated out- worried that a name beginning with The (ex. Theodore's Paint Company) would be incorrrectly identified and removed, which is why I also included the 4th character as blank. Thank you try67 for your response!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

 

This does exactly what you asked for: 

 

event.value = this.getField("OldbusinessName").valueAsString.replace(/^the/i,"").replace(/\d+/g,"");

 

However, there are a couple of complications you failed to mention in your explaination but included in the example, i.e. the removal of puctuation and white space around the numbers and the word "the".  To write a successful regular expression you must be explicit and precise in the description of what you want to do.

 

Read this:

https://www.pdfscripting.com/public/Pattern-Matching-with-Regular-Expressions.cfm

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

yes, I read it today- where I first heard of reg expressions and the like- I've never written code before Acrobat and really enjoying it and the Community. Thank you Thom and try67 (and Community)!!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

Thom; in the custom calc dialog of NewbusinessName field, I entered your script plus what I was ultimately trying to achieve, the first letter in a business name exclusive of numbers and "The " as the value for NewbusinessName field. Not returning anything, not even an error. Any suggestion? Thanks in advance!

 

var OLDNAME = this.getField("OldbusinessName").valueAsString.replace(/^the/i,"").replace(/\d+/g,"");
var firstChar = OLDNAME.charAt(0);

if (OLDNAME == "") {event.value = "";}
else {event.value = firstChar; }

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

Here's my latest attempt using if/else. It only gets rid of the numbers, not 'the' if it were present too. Please any ideas how to tackle this?????

 

var OLDNAME = this.getField("OldbusinessName").valueAsString;
var firstChar = OLDNAME.charAt(0);
var secondChar = OLDNAME.charAt(1);
var thirdChar = OLDNAME.charAt(2);
var fourthChar = OLDNAME.charAt(3);

if (OLDNAME == "") {event.value = "";}
else if (firstChar === 0||1||2||3||4||5||6||7||8||9) {event.value = OLDNAME.replace(/[^A-Za-z]+/g, '');}
else if ((firstChar === "T"||"t") && (secondChar === "H"||"h") && (thirdChar === "E"||"e") && (fourthChar === "")) {event.value = OLDNAME.slice(4).replace(/[^A-Za-z]+/g, "");}

else {event.value = firstChar; }

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

The code operates correctly. It does exactly what it is supposed to do as it is written. I think you need to go back to my first comment about being explict and precise, and the additional complication. Both the word "the" and the numbers are replaced with empty strings, so the value in the "OLDNAME" variable is "  stop". Note the first two characters are spaces.  So, the value of the calculation is a single space.  In programming you get exactly what you code, nothing more, nothing less.

 

If you want to remove leading spaces, then an additional text replacment is needed. 

 

var OLDNAME = this.getField("OldbusinessName").valueAsString.replace(/^the/i,"").replace(/\d+/g,"").replace(/^\s+/,"");

 

Figuring out all the small details that need to be handled for proper operation of your script can be frustrating. One of the most helpful tools for doing this is the Acrobat Console Window. Using the console window you can run the line of code above over and over, trying out different variations in the regular expressions till you get it all worked out. You wouldn't need to  ask about the first letter not being what you think it should be, because you'd immediately see it when testing the code in the console window. 

You'll find a tutorial here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2021 Mar 14, 2021

Copy link to clipboard

Copied

LATEST

yeah, I went back and step-by-step rebuilt my scenario using your article- thanks!!! it was easier to read after you pointed out my variable flaws.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines