Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to replace all instances of one character in a string

Engaged ,
Mar 14, 2022 Mar 14, 2022

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.

TOPICS
JavaScript
3.8K
Translate
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
3 ACCEPTED SOLUTIONS
Community Expert ,
Mar 14, 2022 Mar 14, 2022

Drop the quotes from the RegExp and it will work.

View solution in original post

Translate
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
Community Expert ,
Mar 15, 2022 Mar 15, 2022

Use this:

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

View solution in original post

Translate
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
LEGEND ,
Mar 15, 2022 Mar 15, 2022

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.

View solution in original post

Translate
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
LEGEND ,
Mar 14, 2022 Mar 14, 2022

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

Translate
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
Engaged ,
Mar 14, 2022 Mar 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.

Translate
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
Community Expert ,
Mar 14, 2022 Mar 14, 2022

Drop the quotes from the RegExp and it will work.

Translate
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
Engaged ,
Mar 14, 2022 Mar 14, 2022

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

Translate
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
Community Expert ,
Mar 15, 2022 Mar 15, 2022

Works just fine for me:

 

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

 

The result:

 

try67_0-1647335866204.png

 

Translate
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
Engaged ,
Mar 15, 2022 Mar 15, 2022

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.

Translate
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
LEGEND ,
Mar 15, 2022 Mar 15, 2022

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.

Translate
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
Community Expert ,
Mar 15, 2022 Mar 15, 2022
LATEST

You must escape special characters. 

Translate
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
Community Expert ,
Mar 15, 2022 Mar 15, 2022

Use this:

var strng = "George Smith Patton";
strng = strng.replace(/ /g, "");
Translate
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