Copy link to clipboard
Copied
I read that the suggested means to replace every instance of a single character in a string is the following example, where the intent is to remove every space from the string:
var strng = "George Smith Patton";
strng = strng.replace(/" "/g, "");
The intent is to yield "GeorgeSmithPatton" as the value of strng. However, this apparently in not the correct syntax in the Acrobat environment, and I've been unsuccessful in my search for how to use a global flag in the .replace( ) parameter. I know how to accomplish the result with a loop, but if there is a means to accomplish the objective with the .replace( ) method I would appreciate help from the Community.
Copy link to clipboard
Copied
Drop the quotes from the RegExp and it will work.
Copy link to clipboard
Copied
Use this:
var strng = "George Smith Patton";
strng = strng.replace(/ /g, "");
Copy link to clipboard
Copied
Regular expressions are quite complex and you need to know the rules for the search string. For instance "." means "any single character" and "$" means "at the end of the string". Other special characters include [ ] ( ) \ * + ? ^ depending on context.
So, replacing all "." with a blank will replace every character with a blank. This is working as intended.
Copy link to clipboard
Copied
Are you hoping this will change a string you can see on the page? Doesn't do that.
Copy link to clipboard
Copied
That is not the purpose. After the replace method changes var strng to 'CamelCase,' strng is used in a subsequent line of script like this:
var fileName = strng + ".fdf";
then concatenated with var path and var folder values and deployed in a folder level privileged script as part of the cPath parameter of exportAsFDF() to save the fdf file to a client folder.
Copy link to clipboard
Copied
Drop the quotes from the RegExp and it will work.
Copy link to clipboard
Copied
Thank you. I tried your suggestion but it did not work.
Copy link to clipboard
Copied
Works just fine for me:
var strng = "George Smith Patton";
strng = strng.replace(/ /g, "");
app.alert(strng)
The result:
Copy link to clipboard
Copied
My mistake; apoligies, you are correct.
However, now I have a follow up question because the global replace does not seem to work with a dollar sign or a period. I tested this global replace technique on strings, numbers, periods, !, &, $ and % using each with this syntax: strng.replace(/%/g, "" ). It works with the things I've tested except periods or dollar signs, and if I include a global replace of a period, the resultant value of var strng is completely blank. However, .replace( ) will work on a dollar sign and a period if I ask it to replace just the first instance of those, like this: strng.replace("$", "") and strng.replace(".", ""). Therefore there seems to be an exception with using the global flag technique on dollar signs and periods, and perhaps other punctuation that I have not tested.
Copy link to clipboard
Copied
Regular expressions are quite complex and you need to know the rules for the search string. For instance "." means "any single character" and "$" means "at the end of the string". Other special characters include [ ] ( ) \ * + ? ^ depending on context.
So, replacing all "." with a blank will replace every character with a blank. This is working as intended.
Copy link to clipboard
Copied
You must escape special characters.
Copy link to clipboard
Copied
Use this:
var strng = "George Smith Patton";
strng = strng.replace(/ /g, "");
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more