Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Replace a character in the string using JavaScript

Avatar

Level 2

Hello,

I would like to ask for help. I have a field in the xml file, which contains a string, that is bound to a text box on the form.

What would be the syntax to replace a special character in the string with the carriage return?

Thank you.

14 Replies

Avatar

Former Community Member

The following script uses a regular expression to identify any white space "\s" character and replace it with a carriage return. You could replace the "\s" with the applicable expression for your special character. Place the expression between the two forward slashes.

var str = this.rawValue;
this.rawValue = str.replace(/\s/,"\n");

Steve

Avatar

Level 4

Is there also a short possibility to search for a special character in a value?

Like if it contains an "a" = "true".

If it contains no "a" = "false".

I want to create a similar script with no variable.

this.rawValue = this.rawValue.replace(/ü/,"ue")

Works for the first character, but not for the following ones. So I want to create a while function.

Avatar

Level 2

Steve,

Thanks a lot.

It worked.

Best regards,

Ilya.

Avatar

Former Community Member

Try these...

Find a character and return a boolean

if (findChar(this.rawValue)) {
    xfa.host.messageBox("Found 'a'.");
}

function findChar(str) {
    var foundChar = false;
    for (var i=0; i < str.length; i++) {
        if (str.charAt(i) == "a") {
            foundChar = true;
        }
    }
    return foundChar;
}

Find a single character and replace it with a string


var str = this.rawValue;
var wordArray = str.split("ü");
this.rawValue = wordArray[0] + "ue" + wordArray[1];

Steve

Avatar

Level 2

Steve,

Thanks a lot. I will try this one as well.

Best regards,

Ilya.

Avatar

Level 4

Find a single character and replace it with a string


var str = this.rawValue;
var wordArray = str.split("ü");
this.rawValue = wordArray[0] + "ue" + wordArray[1];

Didn't work, when the same char was more than one time in the field.

But the bolean did the job.

Thanx

Lisa

Avatar

Level 2

Lisa,

Thanks a lot. It worked.

Regards,

Ilya.

Avatar

Level 4

Huh?

I didn't post my script...

It sure is no clean programming since I just took Steve's solutions and delted the ligns I didn't want (the app allert)

Though in the end I've taken the boolean out, or better replaced it with the replacement function, it repeats the replacement no matter how much "ü"'s are in there. (I needed the boolean though, since as long as it was true, the if should be executed.)

Somehow it worked... not 100% sure why, but it worked.

if (findChar(this.rawValue)) {
   

}


function findChar(str)

{  for (var i=0; i < str.length; i++)

     {
        if (str.charAt(i) == "ü")

          {
           this.rawValue = this.rawValue.replace(/ü/,"ue")
          }
     }

}

Avatar

Level 2

I used this approach to replace one of the purposely embedded character '~' into the XML file during the XSLT transformation and replace it using JavaScript with the carriage return.

Thank you.

Avatar

Former Community Member

This will handle multiple occurences of  "ü".

var str = this.rawValue;
var wordArray = str.split("ü");
var wordIndex = wordArray.length - 1;
str = "";
for (i=0; i < wordIndex; i++) {
    str += wordArray[i] + "ue";
}
this.rawValue = str + wordArray[wordIndex];

Avatar

Level 2

Steve,

Thanks a lot for this example.

It looks that I will need to use this for my current project and use a lot of it.

Thanks again.

Ilya.

Avatar

Level 10

llya

You could also use the global match attribute of the RegExp ... that is the "g" placed after the RegExp

.replace(/ü/g,"ue")

Avatar

Level 2

Thanks a lot. I will try this as well.

Avatar

Level 1

FYI, Steve L Walker​'s original code:

    this.rawValue = str.replace(/\s/,"\n");

Actually replaces any whitespace "\s" character with a newline "\n" character, not a carriage return "\r" character as stated in the answer and requested by the OP. While the newline character the OP got is likely what they actually needed, an OSX and Linux end-of-line sequence, the statements seem to indicate the opposite has occurred.

To replace any whitespace "\s" character with a carriage return "\r" character, use an "\r" instead of a "\n":

    this.rawValue = str.replace(/\s/,"\r");

Note: This is a valid end-of-line sequence for legacy Mac systems, but not for any modern OS. DOS and Windows use "\r\n", while Linux and OSX use "\n".

To replace any whitespace "\s" character with a carriage return "\r" character AND a newline "\n" character (a Windows end-of-line sequence) use an "\r\n":

    this.rawValue = str.replace(/\s/,"\r\n");