Home > Mobile >  Javascript ignore html tag and add space to the string after every 2 characters
Javascript ignore html tag and add space to the string after every 2 characters

Time:11-16

I have a string with HTML in between. What I want to achieve is add a space in between after every 2nd character.

For example for input like below -

'<span>234567</span><span>34526754</span>'

'<span>23 45 67</span><span>34 52 67 54</span>'

How can I achieve this in JavaScript?

CodePudding user response:

{23 45 67}{34 52 67 54} try this

CodePudding user response:

let str = '<span>234567</span><span>34526754</span>'

str.match(/(?<=\<span>).*?(?=<\/span>)/g).forEach(s=>{
 str = str.replace(s, s.match(/.{2}/g).join(' '))
})
console.log(str)

  • Related