Alert of Altered String

Asked

Viewed 62 times

0

I have the following code:

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

alert("Valor Original: " & ??????); -> No caso aqui eu deveria mostrar 000.000.00 01
alert("Valor Novo: " & str_subs);

I need to show the ORIGINAL Value that replace found before changing, how should I proceed?

  • Why overwrite the variable value? can’t give different names to the original version and the changed one? And by the way the javascript concatenation character is the +, must correct here: alert("Valor Novo: " & str_subs);

  • @Sergio, corrected thanks. I need to get the value he found inside the string, you know ? I mean, the original value.

  • And why not do var novaString= string.replace(/\d\ ....etc and thus maintain the variable string with the original value?

1 answer

3


You could be doing something like this:

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

alert("Valor Original: " + old);
alert("Valor Novo: " + to_replace);

Demo

  • perfect friend, thank you!

Browser other questions tagged

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