Skip to main content
August 17, 2013
Answered

regexp

  • August 17, 2013
  • 2 replies
  • 847 views

im trying to remove(technically condensing) duplicated strings

   var str:String = ActionLoader.data;(lets pretend it reads <BREAK><BREAK><BREAK>hello)

  

   var myPattern:RegExp = /<BREAK><BREAK><BREAK>/g;

   str.replace(myPattern, "<BREAK>")

   var myPattern:RegExp = /<BREAK><BREAK>/g;

   str.replace(myPattern, "<BREAK>")

  

   trace("ActionLoader.data=["+str+"]");

i guess i dont fully understand regexp because this made since to me until i tried it

also tried just the word BREAK without the <> incase it was some sort of escape string nonsense

and tried tinkering with qouting the area between the /"forwardslashes"/

to no avail however the string will remain unmodified,anyone know what i've done wrong?

thanks

This topic has been closed for replies.
Correct answer Ned Murphy

The replace method returns a new string, it does not modify the string it acts upon.

Try:

    var myPattern:RegExp = /<BREAK><BREAK><BREAK>/g; 

    trace(str.replace(myPattern, "<BREAK>"));

or reassign string to the processing of the method, as in...

   var myPattern:RegExp = /<BREAK><BREAK><BREAK>/g;

   str = str.replace(myPattern, "<BREAK>"));

In case your thinking is off at the end, in none of what you show are you changing the value of the ActionLoader.data

2 replies

Inspiring
August 17, 2013

In addition to syntax corrections, you can use a single pattern to cover all number of duplicate occurrences instead of trying to figure how many duplicates code may encounter:

var myPattern:RegExp = /(<BREAK>)+/g;

var str:String = "<BREAK><BREAK><BREAK>hello";

str = str.replace(myPattern, "$1");

trace(str);

str = "<BREAK><BREAK><BREAK><BREAK><BREAK><BREAK>hello";

str = str.replace(myPattern, "$1");

trace(str);

str = "<BREAK><BREAK><BREAK><BREAK><BREAK><BREAK>hello<BREAK><BREAK><BREAK>blah";

str = str.replace(myPattern, "$1");

trace(str);

Ned Murphy
Ned MurphyCorrect answer
Legend
August 17, 2013

The replace method returns a new string, it does not modify the string it acts upon.

Try:

    var myPattern:RegExp = /<BREAK><BREAK><BREAK>/g; 

    trace(str.replace(myPattern, "<BREAK>"));

or reassign string to the processing of the method, as in...

   var myPattern:RegExp = /<BREAK><BREAK><BREAK>/g;

   str = str.replace(myPattern, "<BREAK>"));

In case your thinking is off at the end, in none of what you show are you changing the value of the ActionLoader.data

August 17, 2013

i see what you mean, thanks again ned

Ned Murphy
Legend
August 17, 2013

You're welcome