Well, first I should explain a little about what’s going on. String.replace isn’t really what’s doing most the work here, it’s regular expressions that are the real magic.
// what I was explaining: var newStr: String = str.replace(/"(.*?)"/gs, '<i>$1</i>'); // can be better shown as var regex: RegExp = /"(.*?)"/gs; var newStr: String = str.replace(regex, '<i>$1</i>');
/pattern/flags is the shorthand syntax for creating a RegExp object (regular expression), much like “string” is the shorthand syntax for creating a String object.
Regular expressions are a whole language of their own to learn, and have their own logic and methodologies. They are important because they are a lightning fast way to do things that could otherwise take up a TON of processing time.
The AS3 class RegExp itself is here but it doesn’t go into the actual “language” of it much. Adobe has a “basics of regular expressions” type thing here and one of the most useful tools I’ve seen for creating/testing them is here.
But the best thing about regular expressions is that they aren’t something unique to AS3 . The are a part of just about every decent programming language ever made going back a long ways, and the syntax between languages is pretty standard (I’ve had a lot of luck with making PHP regexp’s work in AS3 especially), and it’s the defacto standard way of looking for patterns in strings. I can 100% guarantee you that if this is for a Wikipedia API then someone else has already written all the regular expressions you need plus some if you search for them.
Few more question, I want to change something from File: to | I am having trouble for example “File:this is a file.jpg|Something” to “Something”
Also I am having trouble to change "heart". into something like “heart”.
Additionally is there a way to remove white space + text from point to point or point to end of file?P.S Never worked with such string replacement before sorry for all the questions.
Well, sorta works. The thing is that dealing with regular expressions can range from very simple usage (like making a pattern a and using String.replace to replace stuff like we were doing) to more complex logic where you use the RegExp.exec method to iterate through matches 1 at a time and perform mult-level matching on stuff.
I’m not claiming I’m such a master of regular expressions that I can say for 100% sure that no one can make a single pattern that does that, but off the top of my head I’d say you need to get into iterating through the matches with the RegExp.exec function for that because of the need to both find the pattern, but also to find patterns within the pattern (whitespace within the matched sequence).
But I can get you 90% of the way there with using the simpler String.replace execution:
var str:String = 'File:this_is_a_file.jpg|Something'; var reg: RegExp = /File:(.*?)\|/g; str = str.replace(reg, '<img src="$1" />'); trace(str);
The difference of course being that it doesn’t automatically turn whitespace into underscores in the result, the filename would have to be properly typed to begin with.
Also how to say that I want to replace from | to first space? This doesnt seem to work becuase it doesn’t stop on the first space it takes all the spaces there are ;Pstr = str.replace(/\|(.*?) /gs, '');
Actually that works for me.
var str:String = 'File:this_is_a_file.jpg|Something after the space'; var reg: RegExp = /\|(.*?) /g; str = str.replace(reg, ''); trace(str); // File:this_is_a_file.jpgafter the space
Keep in mind that the first space is part of the pattern match, so therefore will get replaced as well. If you want to match including the first space, but keep a space, then do the same match, but instead of ’’ use ’ ’ as the replacement (add the space back in that’ll be deleted as part of the pattern found.)
var str:String = 'File:this_is_a_file.jpg|Something after the space'; var reg: RegExp = /\|(.*?) /g; str = str.replace(reg, ' '); trace(str); // File:this_is_a_file.jpg after the space