正则表达式每4个字符后插入空格?

时间:2015-08-03 09:36:10

标签: regex vbscript

使用VBScript 5.5 Regex,如何替换字符串,以便每4个字符后有一个空格?

我尝试了以下但不起作用:

Dim rex As New RegExp

rex.IgnoreCase = True
rex.Global = True
rex.Pattern = ".{4}"

Dim newString as String
newString = Trim$(rex.Replace(Trim$(inputString), "$0 "))

1 个答案:

答案 0 :(得分:3)

你需要group()和更好的ref($ 1):

>> Set r = New RegExp
>> r.Global = True
>> r.Pattern = "(.{4})"
>> WScript.Echo r.Replace("1234567890123", "$1 ")
>>
1234 5678 9012 3
>>