question

Serge A avatar image
1 Like"
Serge A asked Phil BoBo commented

String .replace method: the number of occurences replaced

Currently, .replace method of the string objects is supposed to replace all occurrences, but replaces only the first one. It is probably a bug:

In the same time, this unintended behavior can be actually useful. Many other programming languages use a count parameter of the string replacement method or implement two methods. Some examples: Python has a count parameter string.replace(), Java implements two methods String.replaceFirst() and String.replaceAll(), C# has a count parameter in Regex.Replace(), Javascript allows both by accepting regular expressions as search objects.

In Flexscript there are only two options:

  • use stringreplace() to replace all occurences
  • abuse the bug in .replace() method to replace only the first one

The second option is not particularly elegant. I suppose either the method or the documentation should be fixed, and the complementary method or an optional count parameter should be added.

FlexSim 17.0.3
flexscriptstring
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Matthew Gillespie avatar image
2 Likes"
Matthew Gillespie answered Phil BoBo commented

This is a documentation error. The replace method uses regular expressions so you need to use /o/g to replace all instances of o.

Here is the updated documentation that will be in 17.0.4:

string.replace

string replace( string findStr , string replaceWith )
string replace( RegExp findPattern , string replaceWith )

Parameters

findStrThe string to look for.
replaceWithThe string to replace findStr with.
findPatternThe pattern of characters to look for, a regular expression.

Returns

stringA copy of the string with a find pattern replaced with a new string.

Description

Replaces a series of characters with another.

While you can use a regular string for the findStr, this will only replace the first instance of findStr.

  1. string text = "I want you to want me.";
  2. return text.replace("want", "need"); // "I need you to want me."

In order to replace all instances of a string you need to use a regular expression. The regular expression is defined between the forward slashes. Note the g modifier that modifies the search pattern to apply to all occurences. See the following section on Regular Expressions for more details.

  1. string text = "I want you to want me.";
  2. return text.replace(/want/g, "need"); // "I need you to need me."

Regular Expressions

A regular expression is a concise way of specifying a pattern of characters to look for. You start and end a regular expression with a forward slash "/" (similar to how you start and end a string with quotes) and then add modifiers:

  1. /pattern/modifiers

Here is a brief explanation of some regular expression syntax.

Modifiers

These come after the closing / of the regular expression and modify their behavior

g - Global Match, matches all occurences, and not just the first one.

i - Case-insensitive match.

Brackets

[] - Matches any character in the brackets. Can use a dash to specify a range of characters.

  1. [abc] - matches an a, b or c.
  1. [a-z] - matches any lowercase letter.
  1. [0-9] - matches any numerical digit.
  1. [^abc] - matches any character that is not an a, b or c.

Or

| - Matches any of the alternatives separated by a |.

  1. (abc|cba)- matches the sequence "abc" or "cba".
  1. (gray|grey) or gr(a|e)y - matches "gray" and "grey".

Quantifiers

Define the quantity of characters the preceding expression will match

* - Matches any string that has zero or more of the specified characters.

  1. ab*c - matches "ac", "abc", "abbc", "abbbc", etc. Basically any sequence of an a, any number of b's, and then a c.

+ - Matches any string that has at least one of the specified characters.

  1. ab+c - almost the same as ab*c except that "ac" no longer matches.

? - Matches any string that has at zero or one of the specified characters.

  1. colou?r - matches both "color" and "colour".

Repetitions

{n} - Matches n number of occurences.

{m,n} - Matches at least m, up to n number of occurences.

Periods

. - Matches any character.

\. - Matches a period.

Examples

This code removes all numbers. We are using [0-9] to find characters in the numeric range and then we put a plus after it to find any sequence of numbers no matter how long. Finally we add the g modifier to match all occurences.

  1. string str = "abcd123efg456hij789klm0";
  2. string letters = str.replace(/[0-9]+/g, ""); // "abcdefghijklm"

Replace any instances of blue or green regardless of case with red.

  1. string text = "Blue cars are really green.";
  2. return text.replace(/blue|green/gi, "red"); // "red cars are really red."

Replace all 3 letter words starting with b and ending with t.

  1. string text = "My favorite words are: bit bat but bot bet.";
  2. return text.replace(/b[aeiou]t/g, "Money"); // My favorite words are: Money Money Money Money Money.

Hide any email addresses. Note that "\." is how you specify you want to see a period and not just any character.

  1. string text = "Email me at sim-guy34@gmail.com or dev@flexsim.com";
  2. return text.replace(/([a-z0-9_\.-]+)@([0-9a-z\.-]+)\.([a-z\.]{2,6})/g, "###"); //Email me at ### or ###
· 10
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.