Skip to main content
lenny_bouh
Inspiring
July 29, 2024
Answered

Expression : replace()

  • July 29, 2024
  • 1 reply
  • 811 views

Hi,

I am trying to get as much information as possible about the expression .replace() .

 

I looked the Ae expression documentation, forums, asked Adobe assitants, but never found official and complete informations about this expression. So If you have a link or informations yourself, feel free to let us all know in the comment, I'll actualise this post.

Note that I'm not a pro and English is not my first language, so if there are errors, tell me what to change.

 

Here are the informations I have already :

// g : search (g)lobally for the string
// i : search while been case (i)nsensitive

var a1 = [numerical or alphabetical value];
var a2 = [second numerical or alphabetical value];
...
var b = [numerical or alphabetical value inside ""];

text.sourceText.replace(/a1|a2/gi, b)

 

exemples :

text.sourceText.replace(/e/gi, 3)
// before : HELLO everyone
// after : H3LLO 3v3ryon3
______________
text.sourceText.replace(/e/, 3)
// before : HELLO everyone
// after : HELLO 3veryone
______________
text.sourceText.replace(/E|o/g, 1)
// before : HELLO everyone
// after : H1LLO every1ne
______________
text.sourceText.replace(/e/gi, 3).replace(/o/gi, "@")
// before : HELLO everyone
// after : H3LL@ 3v3ry@n3

 

  • It seems to work only on Source text and not numeric values. Even if it can replace numbers.
  • When not using "g" it only works on the first character found, even if you search for severals characters with "|".
  • $ seems to be an important. It displays "[Helper Object]".

 

 

Thanks to :

Dan Ebberts (on Creative Cow)
Lloyd Alvarez (on Creative Cow)

This topic has been closed for replies.
Correct answer Dan Ebberts

That's correct. It's a String method from core JavaScript.

1 reply

Mylenium
Legend
July 29, 2024

The syntax inside the parentheses is simply based on regular expressions. Letters like g, w, b, i etc. define search parameters/ filters, the actual strings are followed by that. Numbers would be filtered with \d as in "digits", stuff like $ defines the start of the search (string anchor) and would not be necessary for single words, but may be relevant for complex search patterns. There's a ton of other stuff. RegEx can be powerful, but is also hard to understand. I suggest you look it up in a generalized web search.

 

Mylenium

Dan Ebberts
Community Expert
Community Expert
July 29, 2024

Yes, the JavaScript implementation of regular expressions (RegExp) is what you're looking for. There's a nice section in my favorite JavaScript book "JavaScript The Definite Guide" by David Flanagan.