I am currently receiving data from the backend, a string of numbers (accountNumber), which has 10 digits, and is specific to each user. where the first is 1, then 0... and lastly the accountNumber (it can contain 1 digit or more) Ex: 1000000032 / 1000002032 / 1000000004. The idea is to return this string with the digits of the accountNumber in bold, Ex: 10000000 32 / 100000 2032 and so on. What I tried so far : Using this as ex: 1000002032
const clean = accountNumber.substring(1, 10) ==> '000002032'
const showOnlyAccountNumbers = clean.replace(/^0+/, ''); ==> '2032';
const transformToArray = showOnlyAccountNumbers.split();==> ['2032']
const boldNumbers = showOnlyAccountNumbers.map((each) => {
return <b>{each}</b>
}) ==> {[...]}
I am not getting the result since the .map returns an array of objects and not what I need to be able to somehow join that in a string with the rest.
First of all, Thanks !
One way to do this is by getting the index of the first nonzero element. And we apply the tag
bold
to the substring that exists from that index.If the string does not have any nonzero number, the same string is returned.
To achieve this, we use the method
substring
and convert the resulting substring to a number, to remove leading or irrelevant zeros. Once the numerical value is obtained, we convert it into a string and pass it as an argument to the methodindexOf
, in this way we obtain the index at which said substring begins in the complete string.Finally we apply the changes from that index and return the result.
I have created this example that you can adapt according to what you need:
I hope this helps you solve the problem.
Edition
Usually, when I say that you can adapt it to your particular situation, the idea is that you extract the essential part of the solution. However, since it seems that it is not clear how to do it, I show you that it is simply a question of creating the following function:
Which you can use in any string that you pass as an argument, for example:
Well, they already gave you the answer, but I still wanted to make my contribution.
The variable that contains
accountNumber
the I convert it to an array with the spread operator[...]
followed by that I iterate it and withfindIndex
the search for the half that divides the numbers that are unequal to 1 and 0 , now having that division I can divide the array in 2 with the methodslice()
Result
At the end I concatenate them with literals template and I put the final array in bold.
HTML
RESULT