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

How to replace all instances of a set of characters (including line breaks) in the source text?

Explorer ,
Mar 09, 2024 Mar 09, 2024

Another question ragarding .replace: I want to remove the line breaks before and after all "'*" characters in a layer source text:

"Text 1
*

Text 2
*
Text 3"

should turn into "Text 1*Text 2*Text 3". I've recently learned that .replace only affects the first instance, and needs to be complemented by a /g (as in "global"). However, I can't find out exactly how to make this work. This is my best attempt:

text.sourceText.replace(/\r*\r/g, "*")

  

but it doesn't work. What am I doing wrong here?

TOPICS
Expressions
525
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

Advocate , Mar 09, 2024 Mar 09, 2024

Try this:

text.sourceText.replace(/\r\*\r+/g, "*")
Translate
Advocate ,
Mar 09, 2024 Mar 09, 2024

Try this:

text.sourceText.replace(/\r\*\r+/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
Explorer ,
Mar 09, 2024 Mar 09, 2024

Thank you, it works! What is the "+" about? For me it works without it as well.

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
Advocate ,
Mar 09, 2024 Mar 09, 2024
LATEST
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