Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
i see what you mean, thanks again ned
Copy link to clipboard
Copied
You're welcome
Copy link to clipboard
Copied
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);
Find more inspiration, events, and resources on the new Adobe Community
Explore Now