### Replace using RegEx

With the help of JavaObject (requires the JavaObject Library), a powerful replace method usign JavaScript Replace can be developed.
Sub Replace Text
`Sub JSReplace(pattern As String, Target As String, Replacement As String) As String
Dim Result As String = ""
Dim joSEM As JavaObject
Dim script As String = $"function repl() {var re = ${pattern};var str = "${Target}"; return str.replace(re, '${Replacement}');} repl();"$
joSEM.InitializeNewInstance("javax.script.ScriptEngineManager", Null)
Try
Result = joSEM.RunMethodJO("getEngineByName", Array("nashorn")).RunMethod("eval", Array(script))
Catch
Log($"[ERROR] - ${LastException.Message}"$)
End Try
Return Result
End Sub`
Example removing an XML block from HTML string
Dim Str as String
Str = $"This is an XML block. This is the text after the XML block
..."$
Log(Str)
Str = JSReplace("/[\S\s]+?<\/xml>/gi", Str, "")
Log(Str)
Output
This is an XML block. This is the text after the XML block
...
This is the text after the XML block
...
Note
There is a built-in Regex.Replace method:
Dim Str As String = $"This is an XML block. This is the text after the XML block
..."$
Log(Regex.Replace("[\S\s]+?", Str, ""))

Changed: , Category: JavaScript, File: T6C209M1531.htm, ID: 1531