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

Replace Special Characters in a String

Explorer ,
Aug 31, 2010 Aug 31, 2010

Let's say someone copies the folowing list and pastes it into a Javascript prompt box:

302304

305678

245675

How do I manipulate the string so it reads:

302304 305678 245675

In other words, how do I replace the manual line breaks in a string with spaces?

Thanks!

TOPICS
Actions and scripting
2.1K
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

correct answers 1 Correct answer

LEGEND , Aug 31, 2010 Aug 31, 2010

This is the cleaner method you were looking for:

/[\n\r]/

In this situation, even a simple \s would work, but that might not be what the OP was looking for...

Harbs

Translate
Adobe
Guide ,
Aug 31, 2010 Aug 31, 2010

Im still not very good with these reg exp but this would work although Im sure I missed a cleaner way…

var st = "302304\n305678\r245675"; $.write(st); $.writeln(''); var ns = st.replace(/(\r|\n)/g,' '); $.write(ns);

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 ,
Aug 31, 2010 Aug 31, 2010

This is the cleaner method you were looking for:

/[\n\r]/

In this situation, even a simple \s would work, but that might not be what the OP was looking for...

Harbs

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
Explorer ,
Sep 01, 2010 Sep 01, 2010

Thanks!

the final result works:

stringName = stringName.replace(/(\r|\n|\s)/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
LEGEND ,
Sep 01, 2010 Sep 01, 2010
LATEST

\r and \n are both \s characters.

This is functionally equivalent to what you have...

stringName = stringName.replace(/\s/g,' ');

I highly recommend browsing this website (lots of useful info there):

http://www.regular-expressions.info/

Harbs

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