143
var str   = 'asd-0.testing';
var regex = /asd-(\d)\.\w+/;

str.replace(regex, 1);

That replaces the entire string str with 1. I want it to replace the matched substring instead of the whole string. Is this possible in Javascript?

dave
  • 7,717
  • 19
  • 68
  • 100
  • FYI, all of the solutions are pretty complex. You just want to replace the match, right? Easy way to do it, without modifying your regex at all, is like this: ```str.replace(regex, "$`1$'")``` see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter – SgtPooki Oct 09 '20 at 01:34

5 Answers5

162
var str   = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
str = str.replace(regex, "$11$2");
console.log(str);

Or if you're sure there won't be any other digits in the string:

var str   = 'asd-0.testing';
var regex = /\d/;
str = str.replace(regex, "1");
console.log(str);
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • 2
    or using function: 'asd-0.testing'.replace(/(asd-)\d(\.\w+)/, function(mystring, arg1, arg2){return arg1 + 'mynumber' + arg2}) – Ivan Rave Jun 17 '15 at 13:59
  • 1
    is there any answer where you DONT know the structure of the regex? here you are basically creating a new regex with two matches – Martin Massera Jul 05 '16 at 22:36
  • 2
    It is good to know that you need braces () around the part you want as $1, $2 etc. – Bas Slagter Jul 12 '19 at 07:43
62

using str.replace(regex, $1);:

var str   = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;

if (str.match(regex)) {
    str = str.replace(regex, "$1" + "1" + "$2");
}

Edit: adaptation regarding the comment

31

I would get the part before and after what you want to replace and put them either side.

Like:

var str   = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;

var matches = str.match(regex);

var result = matches[1] + "1" + matches[2];

// With ES6:
var result = `${matches[1]}1${matches[2]}`;
Félix Saparelli
  • 8,424
  • 6
  • 52
  • 67
6

I think the simplest way to achieve your goal is this:

var str   = 'asd-0.testing';
var regex = /(asd-)(\d)(\.\w+)/;
var anyNumber = 1;
var res = str.replace(regex, `$1${anyNumber}$3`);
Lantanios
  • 91
  • 2
  • 7
0

Personally, I like the liberty provided by the replacer technique (Search for 'Specifying a function as the replacement' section). This is also simplier to debug in details.

function replacer(match, p1, p2, offset, string) {
    console.log('match = ' + match);
    console.log('p1 = ' + p1);
    console.log('p2 = ' + p2);
    console.log('offset = ' + offset);
    console.log('string = ' + string);
    custom_op = p1 + 1 + p2;

    return custom_op;
}

var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;

matches = str.match(regex)

res = str.replace(regex, replacer);
console.log(res);

// ----OUTPUT----
// match = asd-0.testing
// p1 = asd-
// p2 = .testing
// offset = 0
// string = asd-0.testing
// asd-1.testing

NOTE: To ease the identification of regex groups, consider using https://regex101.com/

Greg7000
  • 297
  • 3
  • 15