Replace a character in the string using JavaScript | Community
Skip to main content
Level 2
Apr 13, 2009 9:31 PM UTC
Question

Replace a character in the string using JavaScript

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

14 replies

Apr 14, 2009 4:01 PM UTC

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

Translate
ishark59Author
Level 2
Apr 14, 2009 2:08 PM UTC

Steve,

Thanks a lot.

It worked.

Best regards,

Ilya.

Translate
Level 4
Apr 14, 2009 2:00 PM UTC

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.

Translate
Apr 14, 2009 1:19 AM UTC

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

Translate