/    Sign up×
Community /Pin to ProfileBookmark

remove everything except {Group} and {Desc} and add a space between them?

how to remove everything except {Group} and {Desc} from string variable cardLabel and add a space between {Group} and {Desc} 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}’;

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 16.2021 — @asifakhtar#1626862 If I understand this correctly the code from your other thread should work without modifications as the function `replace` has no effect when the search string is not found. Give it a try.
Copy linkTweet thisAlerts:
@SempervivumJan 16.2021 — PS: After having read your posting again I'm not shure if I understood it correctly: What value comes from the database, cardLabel or deal (in your other thread)? And which one might be incomplete, cardLabel or deal? Please explain more detailed.
Copy linkTweet thisAlerts:
@JMRKERJan 17.2021 — You are not formatting the template literal strings correctly.

If you don't want the other variables, just don't include them.
<i>
</i>cardLabel=<span><code>${Group} ${Desc}</code></span>;
cardLabel=<span><code>${Desc.padEnd(20)} ${Group}</code></span>;
// OR: cardLabel=<span><code>${Desc.padEnd(20,'-')} ${Group}</code></span>;
Copy linkTweet thisAlerts:
@asifakhtarauthorJan 17.2021 — @Sempervivum#1626865

I get cardLabel’s dynamic value from database and the value can be one of the following 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}';
Copy linkTweet thisAlerts:
@SempervivumJan 17.2021 — I see. In this case my statement above should be applicable:
>the code from your other thread should work without modifications as the function replace has no effect when the search string is not found.
×

Success!

Help @asifakhtar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...