Change specific string fields in Javascript

Asked

Viewed 186 times

2

I have the following string:

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

I need to create a function, that "Search" inside that 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 I will set in another string, for example, if it finds, it arrow "129.000.000 02".

P.S: Remembering that he needs to leave the points and spaces like this one, and only change after the . (points) and spaces, otherwise my report does not read.

  • 1

    Did you ever try anything with RegEx? Perhaps you’d better put the code of your attempt to help answer.

  • 5

    I wonder if you can stop asking questions with exclamations! I’ve edited a couple, and it’s very strange... :/

  • 4

    @brasofilo I edited the others :D

  • 4

    The @brasofilo is so upset that changed the ? of the question itself by an exclamation also :) (pudera, is so ! contaminating :P )

  • OK Amigo...@Dang, I didn’t try with REGEX, because I don’t know how to mount this function to locate a given field within a string, because the values will never be the same, you understand ?

  • 2

    I edited several too! @brasofilo

  • 3

    See, 7605, your exclamations so frying the staff, jjjjj

Show 2 more comments

1 answer

3


You only need a simple substitution with regular expression; the verification of existence or not is already on account of the method replace.

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.

  • 1

    Perfect @Rui-Pimentel, thanks!

  • No reason, I’m glad it worked out!

  • 1

    @user7605 something I realized now... \d in regular expression is a form equivalent to [0-9], but shorter. I think it looks more elegant. In an answer I gave you in another question about regular expression, you can change the [0-9]for \d. I’ll edit there too.

  • Ah, blz @Renan, I saw it on the internet, thanks for the tip.

  • @Renan, do I need to put a character first? i put it this way: string = string.replace(/[0-9].[0-9].[0-9] [0-9]/g, str_subs); and it didn’t work.

  • @user7605 this expression you mention takes numbers in the format "0.0.0 0". You need to add more \d or [0-9] (recommend \d) to make it work. I.e.: /\d\d\d[.]\d\d\d[.]\d\d\d[.]/g

  • @Ruipimentel in regular expressions the dot matches with any character other than line break. If you want to get points explicitly, use \. or [.]. Either way, +1.

  • Fala @Renan! I don’t understand very well... I’m not doing it anymore? I used the version \., is that it’s right along with the \d! :)

  • @Ruipimentel the problem is between my chair and my keyboard. Your code ta correct, was wrong #Milking dynamics

  • Haha capable, man! What a relief I didn’t give you a straight answer!

Show 5 more comments

Browser other questions tagged

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