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?

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 is closed to new replies. Start a new post to keep the conversation going.
Airweb_AECorrect answer
Legend
March 9, 2024

Try this:

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

3 replies

Airweb_AECorrect answer
Legend
March 9, 2024

Try this:

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

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

Translate
Legend
March 9, 2024

Look at this:

The Power of Regular Expressions 

Translate