Change specific string fields in Javascript

2

I have the following string:

string = "Este aqui é o valor do cliente 000.000.00 01";

I need to create a function that "Looks for" within this string that contains letters and numbers if there is a field with this format "000.000.000 00" , if it exists, it changes to the value that I'm going to set in another string, if it finds it, it will "129.000.000 02" .

Q: Remembering that he needs to leave the points and spaces in the same way as there, and only change after the . (points) and spaces, otherwise my report does not read.

    
asked by anonymous 23.05.2014 / 21:07

1 answer

3

You just need a simple replacement with regular expression; the existence check is no longer on the replace method.

An example of replace in JavaScript:

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

I hope it helps.

    
23.05.2014 / 21:26