var str = '1234qwer'; newStr = str.replace(/(1234)(qwer)/, '$24321'); console.log(RegExp.$1); // 4321qwer console.log(RegExp.$2); // qwer console.log(newStr); //qwer4321