145
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?

1

6 Answers 6

165

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);

Sign up to request clarification or add additional context in comments.

3 Comments

or using function: 'asd-0.testing'.replace(/(asd-)\d(\.\w+)/, function(mystring, arg1, arg2){return arg1 + 'mynumber' + arg2})
is there any answer where you DONT know the structure of the regex? here you are basically creating a new regex with two matches
It is good to know that you need braces () around the part you want as $1, $2 etc.
64

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

1 Comment

I want to replace the substring with '1' not the entire string with the substring
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]}`;

2 Comments

+1 I personally like having the collection of matches to frack with.
I agree, having the matched set available is more readable in my opinion.
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`);

Comments

4

A One-Liner based on @Greg7000's answer 👍🏼 which uses a replacer method:

'asd-0.testing'
  .replace(
    /(asd-)\d(\.\w+)/,
    (match: string, p1: string, p2: string) => { 
      return `${p1}1${p2}`; 
    });

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.