RegExp 객체의 Replace 메소드는 문자열에서 패턴을 검색하고 지정된 대체 문자열로 바꾸는 데 사용됩니다.
구문: RegExpObject.Replace(string, replacement)
인수:
string: 검색 및 바꾸기가 수행될 입력 문자열입니다.
replacement: 일치된 패턴을 바꿀 문자열입니다. 또한 정규 표현식에서 캡처된 그룹을 참조하는 특수 대체 토큰(예: $1, $2 등)을 포함할 수 있습니다.
예 1: 단순 바꾸기
Dim regEx, inputStr, result
' Create a new RegExp object
Set regEx = CreateObject("VBScript.RegExp")
' Set the pattern to match the word "cat"
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = "cat"
' Input string
inputStr = "The cat sat on the cat mat."
' Replace "cat" with "dog"
result = regEx.Replace(inputStr, "dog")
'Now result has the value "The dog sat on the dog mat."
예 2: 바꾸기로 그룹 캡처 사용
Dim regEx, inputStr, result
' Create a new RegExp object
Set regEx = CreateObject("VBScript.RegExp")
' Set the pattern to match a date in MM-DD-YYYY format
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = "(\d{2})-(\d{2})-(\d{4})"
' Input string
inputStr = "Today's date is 05-19-2025."
' Swap the date format to YYYY-MM-DD
result = regEx.Replace(inputStr, "$3-$1-$2")
' Display the result
'Now result has the value "Today's date is 2025-05-19."