[Vuejs]-Once replaced, skip that part of string โ€“ Regex Javascript

3๐Ÿ‘

โœ…

If I understood correctly, youโ€™d like to apply regex-replace for one numerical string recursely like string.replace(\regex\, '$1,').replace(\regex\, '$1,').replace(\regex\, '$1,'), but ignored the parts which already replaced.

Below is one solution which uses negative lookahead.

let test = '895645784578457845784578457845' //org string
let test1 = test.replace(/(\d{10}(?!\,))/gm, "$1,")
                .replace(/(\d{10}(?!\,))/gm, "$1,")
                .replace(/(\d{10}(?!\,))/gm, "$1,") 
                // simulate recurse-replace three times
console.log(test1)

test1 += '1234567890123' //new string came
let test2 = test1.replace(/(\d{10}(?!\,))/gm, "$1,")
console.log(test2)

test2 += '1234567890123' //new string came
let test3 = test2.replace(/(\d{10}(?!\,))/gm, "$1,")
console.log(test3)
๐Ÿ‘คSphinx

Leave a comment