AS3删除字符串中单词后面的字符



AS3|Adove Air 3.5|Flash CS6

我已经从一个在线源中提取了html,并将其放入一个String中,我正在将其逐个拆开,以根据提取的信息构建一个XML文件。我需要搜索整个字符串来删除"信息"后面的字符,直到字符"&"。在整个字符串中有多个这样的实例,所以我认为最好使用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"不一致。它们是随机数字和字符,可能还有长度,所以我不能真正使用上面写的内容。它总是会导致一个"&"。

提前感谢您的帮助。

我认为regexp更像

//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]的做作,但应该是这样的。希望这能有所帮助。

最新更新