Take the 2-minute tour ×

AS3 | Flash CS6 | Adobe Air 3.5

I have a very long string that will eventually be formatted as XML. In the string I have several instances of something along the lines of:

<title>Some title</title>

//the following line would need changed
<title>Some title</garbage>

<title>Some title2</title>
<title>Some title3</title>

//the following line would need changed
<title>Someone's title & (his info)</garbage>

//the following line should NOT change
<another title>something else</garbage>

I need to search and replace all of these:

<title>(whatever is here)</garbage>

And make them:

<title>(whatever is here)</title>

As you can see I can't simply search and replace "garbage" beacuse it wouldn't apply in every single instance... I only want to replace the ones that use < title > ...not < another title > Also please note that it is uncertain what characters will be between < title > and < /garbage >

So although I know this doesn't work... I was thinking something along the lines of:

str = str.replace(/<title>\w+<\/garbage>/, "<title>\w+<\/title>");

I think the answer is under "Capturing sub-strings" on this page but so far no luck: http://www.sevenson.com.au/actionscript/testing-regular-expressions/

It seems like at this point I'm just plugging in random things so it makes more sense to just ask for help.

Any advice is appreciated.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Match any symbol after <title> and before </garbage>

str = str.replace(/(<title>.[^<]+)<\/garbage>/, "$1</title>");

Example http://regexr.com?33nog

share|improve this answer
Perfect! Thanks so much. I could not get that right. –  user1129107 Feb 11 '13 at 8:37
I found one problem with this. If the (whatever is here) is only one character long, this expression does not work. Any suggestion on how to fix this? For 2 or more characters it's great. –  user1129107 Feb 12 '13 at 3:12
Edit: Making it non-greedy fixed it. /(<title>.[^<]+)<\/garbage>/, "$1</title>" Becomes /(<title>.?[^<]+)<\/garbage>/, "$1</title>" –  user1129107 Feb 12 '13 at 3:28
add comment

Your Answer


