by John C. Bland II | Sep 16, 2007 | Flash Platform, RegEx
I was working on an app and needed to remove all HTML instances in a string. Well, that same functionality was needed elsewhere so…I created a class.
/**
* String Utility functions
* @author John C. Bland II
* @version 0.1
*/
package utils{
public class StringUtils {
public static function stripHTML(value:String):String{
return value.replace(/<.*?>/g, "");
}
}
} |
/**
* String Utility functions
* @author John C. Bland II
* @version 0.1
*/
package utils{
public class StringUtils {
public static function stripHTML(value:String):String{
return value.replace(/<.*?>/g, "");
}
}
}
To use it you simply:
trace(StringUtils.stripHTML("some html string")); |
trace(StringUtils.stripHTML("some html string"));
Hope this helps someone.
by John C. Bland II | Sep 4, 2007 | RegEx
I had a recent problem where I needed to parse a template file and replace the custom tags with different code, etc. Well, we had regex ready to grab a single line tag (ex – <[MyTag]>) then we had it ready to grab a multiline tag (with content between it). The problem came in when I needed to have nested tags. Lemme show a quick example:
bq. <[MyTag]>
some text
maybe some regular html tags
anything you want here
<[SomeOtherTag]>
some content
[SomeOtherTag]>
<[AnotherTag:Singleline]>
[MyTag]>
Well, the regex we were using would stop at [SomeOtherTag]> instead of [MyTag]>. I tried using lookbacks (or whatever the technical term is) by using \1 but that wasn’t working. Today I was turned on to a few new resources (“RegexAdvice.com”:https://regexadvice.com/ and “Kodos”:https://kodos.sourceforge.net) which helped me shape and mold the regex to a fully functional template parser.
bq. <(\[[a-zA-Z0-9]*\])[^>]*>([\w\W]*?)\1>
I implemented this with C# using the following code:
bq. Regex _contentReg = new Regex(@”<(\[[a-zA-Z0-9]*\])[^>]*>([\w\W]*?)\1>“, RegexOptions.IgnoreCase);
_matchColl = _contentReg.Matches(template);
After this I merely looped over the matches and utilized “Cody’s”:https://blog.xyzpdq.org/ reflection yumminess he had already setup and VOILA! 🙂
Anyways…all that to say…hopefully this regex helps someone else as it took quite some time to find a solution.