Concatenate strings in Javascript

Asked

Viewed 3,816 times

-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 finds and changes the numeric value the final 2 numbers it keeps the original string. In the case up there, it would be like this:

Upshot = 129.000.000 02

What I need:

Upshot = 129.000.000 01 (that 01 would be the last 2 digits of the original string)

  • I swear I tried, but I can’t understand what you need, try to put more context

  • rsrssrs @Felipekm that was good. Come on, I’ll be practical.

  • @Felipekm, I edited the question, see if you understand.

1 answer

3


Use that 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.