Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

regexp

Guest
Aug 16, 2013 Aug 16, 2013

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

TOPICS
ActionScript
782
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 17, 2013 Aug 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

Translate
LEGEND ,
Aug 17, 2013 Aug 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 17, 2013 Aug 17, 2013

i see what you mean, thanks again ned

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 17, 2013 Aug 17, 2013

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 17, 2013 Aug 17, 2013
LATEST

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines