In ActionScript3 i wanted to get the text between 2 quotes from some HTML using a input index value where i would simply increase the 2nd quote characters value by 1. This would be very simple however i have now noticed using indexOf does not seem to work correctly with quotes and other special characters.
So my question is if you have some HTML style text like this:
var MyText:String = '<div style="text-align:center;line-height:150%"><a href="http://www.website.com/page.htm">';
How can i correctly get the index of a quote ” or other special character?
Currently i try this:
MyText.indexOf('"',1)
but after 0 it always returns the wrong index value.
Also a quick additional question would be is there a better way than using ‘ ‘ to store strings with characters like ” inside? So if i had other ‘ characters etc it won’t cause problems.
Edit –
This is the function i had created (usage = GetQuote(MyText,0) etc)
// 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)));
}
The return for GetQuote(MyText,0) is “text-align yet i need text-align:center;line-height:150% instead.
First off, index of the first quote is 11 and both
MyString.indexOf('"')andMyString.indexOf('"',1)return the right value (the latter also works because you don’t actually have a quote at the beginning of your string).When you need to use an single quote inside another one or a double quote inside another one you need to escape the inner one(s) using backslashes. So to catch a single quote you would use it like
'\''There are several ways of stripping a value from a string. You can use the RegExp class or use standard String functions like
indexOf,substretc.Now what exactly would you like the result to become? Your question is not obvious.
EDIT:
Using the
RegExpclass is much easier:Outputs: