pub(crate) fn replace_once<'a>(
        &'a self,
        vm: &mut VM,
        given: &'a str,
        replace: &str,
    ) -> Result<(String, Option<Captures>), RubyError> {
        match self.captures(given) {
            Ok(None) => Ok((given.to_string(), None)),
            Ok(Some(captures)) => {
                let mut res = given.to_string();
                let m = captures.get(0).unwrap();
                vm.get_captures(&captures, given);
                let mut rep = "".to_string();
                let mut escape = false;
                for ch in replace.chars() {
                    if escape {
                        match ch {
                            '0'..='9' => {
                                let i = ch as usize - '0' as usize;
                                match captures.get(i) {
                                    Some(m) => rep += m.as_str(),
                                    None => {}
                                };
                            }
                            _ => rep.push(ch),
                        };
                        escape = false;
                    } else if ch != '\\' {
                        rep.push(ch);
                    } else {
                        escape = true;
                    }
                }
                res.replace_range(m.start()..m.end(), &rep);
                Ok((res, Some(captures)))
            }
            Err(err) => return Err(RubyError::internal(format!("Capture failed. {:?}", err))),
        }
    }
Replaces the leftmost-first match for `self` in `given` string with `replace`.

### return
replaced:String
ruruby/src/builtin/regexp.rs