【问题标题】:AS3 Remove characters after a word in a stringAS3 删除字符串中单词后的字符
【发布时间】:2013-02-06 09:42:31
【问题描述】:

AS3 |多维空气 3.5 | Flash CS6

我已从在线源中提取的 html 放入一个字符串中,我将根据我提取的信息将其逐个拆开以构建一个 XML 文件。我需要搜索整个字符串以删除“info”之后的字符,直到字符“&”。整个字符串中有多个这样的实例,所以我认为最好使用 RegExp。欢迎提出其他建议。

//code from the website put into a full string
var fullString = new String(urlLoader.data);

//search string to find <info> and remove characters AFTER it up until the character '&'
var stringPlusFive:RegExp = /<info>1A2E589EIM&/g;

//should only remove '1A2E589EIM' leaving '<info>&'
fullString = fullString.replace(stringPlusFive,"");

我无法弄清楚的问题是“1A2E589EIM”不一致。它们是随机数字和字符,可能还有长度,所以我不能真正使用上面写的内容。它总是会导致一个“&”。

提前感谢您的帮助。

【问题讨论】:

    标签: regex actionscript-3 flash air flash-cs5


    【解决方案1】:

    我认为正则表达式会更像

    //search string to find <info> and remove characters AFTER it up until the character '&'
    var stringPlusFive:RegExp = /<info>\w+&/g;
    
    //should only remove '1A2E589EIM' leaving '<info>&'
    fullString = fullString.replace(stringPlusFive,"<info>&");
    

    现在,如果您要将字符串解析为 XML,您可以等到您的 XML 结构完成,然后从“信息”节点中删除信息字符串

    //code from the website put into a full string
    var fullString = new String(urlLoader.data);
    
    //parse to XML
    var xml:XML = XML(fullString),
        index:int, text:string;
    
    for each(var info:XML in xml.descendants('info')){
      text = info.*[0].text();
      index = text.indexOf('&');
      if(index != -1){
        info.*[0] = text.substr(index);
      }
    }
    

    我不确定对 *[0] 的影响,但应该是这样的。希望这会有所帮助。

    【讨论】:

    • 是的,问题是在我开始执行 XML 命令之前进入 XML 格式。 /\w+&/g;虽然工作。谢谢!
    猜你喜欢
    • 2014-12-16
    • 2020-07-11
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多