Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Created December 24, 2013 12:38
Show Gist options
  • Save lynxerzhang/8112751 to your computer and use it in GitHub Desktop.
Save lynxerzhang/8112751 to your computer and use it in GitHub Desktop.
修正使用CDATA修饰符书写多行字符的输出格式问题
/**
*
* JacksonDunstan.com 的 http://jacksondunstan.com/articles/2461
* 介绍了在as3中创建"Multi-Line Strings"的方式,在后继评论中叙述了
* 可以使用xml的CDATA标签方便创建多行字符。
*
* 使用xml的CDATA标签创建多行字符最初应该是由 http://ticore.blogspot.com 提出
* 但是创建的多行字符使用trace输出会发现格式有问题(空行的行数)
* 可以使用正则表达式修正
*/
var str:String = <><![CDATA[
AS3 has never had very good support for multi-line strings… until now.
Today’s article discusses the proper and improper ways of writing multi-line strings
and delves into the bytecode so you really understand what’s going on.
please read JacksonDunstan.com's blog (http://jacksondunstan.com/articles/2461)
]]></>
str = str.replace(/^\s*[\r\n]/, ""); //剔除起始换行符
str = str.replace(/[\r\n]\s*$/, ""); //剔除末尾换行符
//str = str.replace(/[\r\n]{2,}/g, "\r\n");
str = str.replace(/[\r\n]+/g, "\n"); //剔除多余空行
str = str.replace(/^\s+/gm, ""); //剔除起始位置字符, 设置multiline修饰符用于多行替换
str = str.replace(/\s+$/gm, ""); //剔除结束位置字符, 设置multiline修饰符用于多行替换
trace(str); //将会输出标准的期望格式
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment