Skip to main content
Known Participant
March 9, 2024
Answered

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

  • March 9, 2024
  • 1 reply
  • 565 views

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?

This topic has been closed for replies.
Correct answer Airweb_AE

Try this:

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

1 reply

Airweb_AECorrect answer
Legend
March 9, 2024

Try this:

text.sourceText.replace(/\r\*\r+/g, "*")
Known Participant
March 9, 2024

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

Legend
March 9, 2024