# AS3 RegExp and String.replace

**URL:** <https://forum.kirupa.com/t/as3-regexp-and-string-replace/217224>\
**Category:** Uncategorized\
**Created:** [February 24, 2007, 1:09am UTC](https://forum.kirupa.com/t/as3-regexp-and-string-replace/217224 "2007-02-24T01:09:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [February 24, 2007, 1:09am UTC](https://forum.kirupa.com/t/as3-regexp-and-string-replace/217224/1 "2007-02-24T01:09:11Z")

</div>

This is an AS3 question (I forgot to put it in the title).

When using the replace method with a regex pattern, is there anyway to have the replace method start back at the beginning of the string after it has found a match.

For example:

```auto
var r:RegExp = /1a1/g;
var s:String = "1a1a1";
trace(s.replace(r, "1")); //1a1

```

The replace method returns 1a1, which also matches the regex pattern but doesn’t get replaced with 1. What I assume happens is that the replace method starts searching the string again _after_ the index of the last replaced match. So, in this case, it will only see a1 after the first replace which doesn’t match the pattern.

I was thinking that I might have to just call the replace method recursively until there are no more matches left, but I was hoping that there would be a better way.

Thanks for any help :?)
