Regex replace number after capture group?

I have a regex pattern:

([0-9]*)xyz

I want to do the substitution as follows:

$10xyz

The problem is that $1- the capture group, but 0- this is just the number that I want to add to the substitution. But the regex thinks I'm asking for a capture group $10instead $1, and then after it is zero.

How can I refer to a capture group and immediately follow it with a number?

Using JavaScript in this case.

UPDATE As mentioned below, my code worked fine. The regular expression tester that I used was accidentally installed on PCRE instead of JavaScript.

+4
source share
2 answers

. JavaScript $10 , 10. , 10 , โ€‹โ€‹ 1, 0.

var r = '123xyz'.replace(/([0-9]*)xyz/, '$10xyz');
console.log(r); //=> "1230xyz"
+3

, -:

var str = "3xyz";
var str1 = str.replace(/([0-9]*)xyz/, "$10xyz");
alert(str1); // alerts 30xyz
+1

Source: https://habr.com/ru/post/1569730/


All Articles