1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to remove everything except {Group} and {Desc} from string variable cardLabel and add a space be

Discussion in 'JavaScript' started by asifakhtar, Jan 16, 2021.

  1. #1
    how to remove everything except {Group} and {Desc} from string variable cardLabel and add a space between them in javascript?

    I get cardLabel value from database and the value sometimes contain all 6 variables but can contain value with sometimes just 2 variables, sometimes with sometimes just 4 variables. Please note order for the variables can be different and can contain more variables in future.

    cardLabel’s dynamic value examples:

    cardLabel='{Group} {Desc} - {termMonth} Month - {termOdometer} KMs/Miles {DeductibleAmount} {DeductibleType}';

    cardLabel='{Desc} - {termMonth} Month - {termOdometer} KMs/Miles';

    cardLabel='{Desc} {Group} - {termMonth} Month';

    cardLabel='{Group} {Desc} - {termMonth} Month {DeductibleAmount}';

    I have tried the following code but it is not ideal because the database value can contain more variables in future:
    cardLabel = cardLabel.replace('{termMonth}','').replace('{termOdometer}','').replace('{DeductibleAmount}','').replace('{DeductibleType}','').replace('-', '');

    The desire end result should be one of the following: cardLabel='{Group} {Desc}'; cardLabel='{Desc} {Group}';
     
    asifakhtar, Jan 16, 2021 IP
  2. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #2
    If I understand this correctly, you are sending values from a server to a client App or WebApp.

    You are facing this kind of issue because you are not sending it with a standard format like JSON or XML.

    Encode your data in JSON on the server and send that JSON. Then depending the frontend project, you can check if the (group, desc, termMonth, Month etc) keys exists and render them accordingly.

    For clarity, what is the frontend and backend language?
     
    Efetobor Agbontaen, Jan 16, 2021 IP
  3. asifakhtar

    asifakhtar Active Member

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    I get the values from the server which I can't change. I run an API end point and get the data. Please note my desire end result should be one of the following: cardLabel='{Group} {Desc}'; cardLabel='{Desc} {Group}'; frontEnd is Angular, Backend is C#
     
    Last edited: Jan 16, 2021
    asifakhtar, Jan 16, 2021 IP