String 获取ActionScript3中特殊字符的索引
在ActionScript3中,我希望使用一个输入索引值从一些HTML中获取两个引号之间的文本,我只需将第二个引号字符值增加1。这将是非常简单的,但是我现在注意到使用indexOf似乎不能正确地处理引号和其他特殊字符 所以我的问题是,如果您有一些HTML样式的文本,如下所示:String 获取ActionScript3中特殊字符的索引,string,actionscript-3,special-characters,indexof,quote,String,Actionscript 3,Special Characters,Indexof,Quote,在ActionScript3中,我希望使用一个输入索引值从一些HTML中获取两个引号之间的文本,我只需将第二个引号字符值增加1。这将是非常简单的,但是我现在注意到使用indexOf似乎不能正确地处理引号和其他特殊字符 所以我的问题是,如果您有一些HTML样式的文本,如下所示: var MyText:String = '<div style="text-align:center;line-height:150%"><a href="http://www.website.com/p
var MyText:String = '<div style="text-align:center;line-height:150%"><a href="http://www.website.com/page.htm">';
但是在0之后,它总是返回错误的索引值
另外一个快速补充的问题是,是否有比使用“”更好的方法来存储包含“inside”等字符的字符串?因此,如果我有其他“字符”等,它不会导致问题
编辑-
这是我创建的函数(usage=GetQuote(MyText,0)等)
GetQuote(MyText,0)的返回值是“文本对齐,但我需要文本对齐:居中;行高:改为150%。首先,第一个引号的索引是11,
MyString.indexOf(“”)
和MyString.indexOf(“”,1)
返回正确的值(后者也有效,因为字符串开头实际上没有引号)
当您需要在另一个引号内使用单引号或在另一个引号内使用双引号时,您需要使用反斜杠转义内部引号。因此,要捕获单个引号,您可以像'\''
有几种方法可以从字符串中提取值。您可以使用RegExp类,也可以使用标准的String函数,如indexOf
、substr
等
现在你到底希望结果是什么?你的问题不明显
编辑:
使用RegExp
类要容易得多:
var myText:String = '<div style="text-align:center;line-height:150%"><a href="http://www.website.com/page.htm">';
function getQuote(input:String, index:int=0):String {
// I declared the default index as the first one
var matches:Array = [];
// create an array for the matched results
var rx:RegExp = /"(\\"|[^"])*"/g;
// create a RegExp rule to catch all grouped chars
// rule also includes escaped quotes
input.replace(rx,function(a:*) {
// if it's "etc." we want etc. only so...
matches.push(a.substr(1,a.length-2));
});
// above method does not replace anything actually.
// it just cycles in the input value and pushes
// captured values into the matches array.
return (index >= matches.length || index < 0) ? '' : matches[index];
}
trace('Index 0 -->',getQuote(myText))
trace('Index 1 -->',getQuote(myText,1))
trace('Index 2 -->',getQuote(myText,2))
trace('Index -1 -->',getQuote(myText,-1))
var myText:String='
索引2-->
索引-1-->
谢谢你提供的信息,我将测试这些。为了回答你的问题,我想到的是创建一个函数GetQuote(MyText,0),它将得到“文本对齐:中心;行高:150%”等。这应该很容易实现,但我希望MyText.indexOf(“”,0)返回11,但MyText.indexOf(“”,1)返回39。我已经用我创建的简单函数更新了我的原始帖子,最初我想避免使用RegExp,并认为它可以正常工作,但它不会返回我想要的内容。@zeddex我编写了一个函数,并将其添加到我的响应中。如果您可以确保html代码是严格的XHTML,您可能希望使用XML类而不是处理字符串。否则,您可能需要编写一个类来处理所需的内容。问题:获取style属性的值是否足够,是否也需要更改它?
// GetQuote Function (Gets the content between quotes at a set index value)
function GetQuote(Input:String, Index:Number):String {
return String(Input.substr(Input.indexOf('"', Index), Input.indexOf('"', Index + 1)));
}
var myText:String = '<div style="text-align:center;line-height:150%"><a href="http://www.website.com/page.htm">';
function getQuote(input:String, index:int=0):String {
// I declared the default index as the first one
var matches:Array = [];
// create an array for the matched results
var rx:RegExp = /"(\\"|[^"])*"/g;
// create a RegExp rule to catch all grouped chars
// rule also includes escaped quotes
input.replace(rx,function(a:*) {
// if it's "etc." we want etc. only so...
matches.push(a.substr(1,a.length-2));
});
// above method does not replace anything actually.
// it just cycles in the input value and pushes
// captured values into the matches array.
return (index >= matches.length || index < 0) ? '' : matches[index];
}
trace('Index 0 -->',getQuote(myText))
trace('Index 1 -->',getQuote(myText,1))
trace('Index 2 -->',getQuote(myText,2))
trace('Index -1 -->',getQuote(myText,-1))