Replace a character in the string using JavaScript | Community
Skip to main content
Level 2
April 13, 2009
Question

Replace a character in the string using JavaScript

  • April 13, 2009
  • 14 replies
  • 22351 views

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

ishark59Author
Level 2
April 15, 2009

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.

_Bruce_Robertson
Level 10
April 21, 2009

llya

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

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

ishark59Author
Level 2
April 21, 2009

Thanks a lot. I will try this as well.

April 18, 2016

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");