-3

I'm trying to bold the text in the first occurence of string, matching the input query but the character is getting bold on every occurence in the string.

Input : "9"
Current Output:9989459
Expected output:9989459

 var  display = "9989459" 
 var input = "9"
 var splitArr = display.split(new RegExp(`(${input})`, 'gi'));
 var result =  splitArr.map((parts,i) => parts === input ? <b key={i}>{parts}</b> : parts)
 return <span> {result} </span>

1 Answers1

-2
const target = '9'
const str = '9989459'
const index = str.indexOf(target)

const parts = [a.slice(0, index), `<b>${a.slice(index, index+1 )}</b>`, a.slice(index + 1)]

// [ '', '<b>9</b>', '989459' ]
Andrés
  • 719
  • 1
  • 4
  • 21
  • No, Its rendering with that b tag , like `9989459` – Barry Allen Jan 20 '21 at 18:46
  • Yes, this is just the example code to extract and transform the original string. Use the resulting array as you wish, for instance to generate html in directly or with any framework – Andrés Jan 20 '21 at 19:05