Help write a rule?

A string of variable length, with 3 characters at the beginning and 3 characters at the end, and the rest of the content is replaced by a "" sign, and the number of "" is the same as the length of the content after removing the first and last 3 characters.

for example, let str = "123456789" becomes 123 * 789

the requirement is easy to realize. I"d like to ask, can it be realized with a regular sentence?

if the length is fixed

str.replace(/(.{3}).{3}(.{3})/, "$1***$2")

can be realized, mainly because the length is not fixed.

Mar.12,2021

'1234564526789'.replace(/(?<=\d{3})\d(?=\d{3})/g, '*')
'1234564526789'.replace(/(?<=.{3}).(?=.{3})/g, '*')

clipboard.png


replace accepts a function as a parameter. If the match is regular, then the first parameter of the function corresponds to $1 , the second corresponds to $2 , and so on. replace uses function as the parameter

let str = "123453333336789"
str.replace(/(^.{3})(.*)(.{3}$)/, "$1*$3")
//"123*789"
Menu