Concatenate strings in Javascript [duplicate]

-2

I have the following function, which changes the numeric value of a string when found:

var original = "Este aqui é o valor do cliente 000.000.00 01";
var original = str.match(/\d\d\d\.\d\d\d\.\d\d \d\d/)
var novo = "129.000.000 02";
original = original.replace(/\d\d\d\.\d\d\d\.\d\d \d\d/g, novo);

What I need to do is that when it encounters and changes the numeric value to the two final numbers it maintains that of the original string. In the case above, it would look like this:

Result = 129.000.000 02

What I need:

Result = 129.000.000 01 (this 01 would be the last 2 digits of the original string)

    
asked by anonymous 24.05.2014 / 00:45

1 answer

3

Use this expression /\d{2}$/ to capture the last two numbers of a string .

var foo = "Este aqui é o valor do cliente 000.000.00 01";
var number = foo.match(/\d\d\d\.\d\d\d\.\d\d \d\d/); // 000.000.00 01
var to_replace = "129.000.000 02";
var lastnumber = foo.match(/\d{2}$/); // 01
to_replace = to_replace.replace(/\d{2}$/, lastnumber);

alert("Valor original: " + number); // 000.000.00 01
alert("Novo valor: " + to_replace); // 129.000.000 01

Demo

    
24.05.2014 / 00:50