Skip to main content
Inspiring
March 14, 2022
Answered

How to replace all instances of one character in a string

  • March 14, 2022
  • 3 replies
  • 3637 views

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.

This topic has been closed for replies.
Correct answer Test Screen Name

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.


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.

3 replies

Bernd Alheit
Community Expert
Community Expert
March 15, 2022

Use this:

var strng = "George Smith Patton";
strng = strng.replace(/ /g, "");
try67
Community Expert
Community Expert
March 14, 2022

Drop the quotes from the RegExp and it will work.

ODuinnAuthor
Inspiring
March 15, 2022

Thank you. I tried your suggestion but it did not work.

ODuinnAuthor
Inspiring
March 15, 2022

Works just fine for me:

 

var strng = "George Smith Patton";
strng = strng.replace(/ /g, "");
app.alert(strng)

 

The result:

 

 


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.

Legend
March 14, 2022

Are you hoping this will change a string you can see on the page? Doesn't do that. 

ODuinnAuthor
Inspiring
March 14, 2022

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.