How to remove Emoji from string using VB

Viewed 321

I need to remove some emoji characters from a string using classic asp and vb script. Here is what I have:

Repeat / Other

Scheduled

Lead

And what I need:

Repeat / Other

Scheduled

Lead

I have been able to remove the emojis using this function but I want to keep special characters such as the forward slash /, spaces, &, :, etc.

Any help is appreciated.

Function strClean (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "((?![a-zA-Z0-9]).)+"
outputStr = objRegExp.Replace(strtoclean, "-")
objRegExp.Pattern = "\-+"
outputStr = objRegExp.Replace(outputStr, "")
strClean = outputStr
End Function
1 Answers

Your current regex matches any char but a line break and ASCII alphanumeric chars. It does not match emojis because VBScript ECMA-262 3rd edition based regex engine cannot match astral plane chars with a mere . pattern.

If you want to just add the emoji matching support to your current pattern, you can replace the . with (?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]) pattern and use

objRegExp.Pattern = "(?:(?![a-zA-Z0-9])(?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))+"

See the regex demo

If you just want to remove all but ASCII chars, you can use

objRegExp.Pattern = "objRegExp.Pattern = "(?:(?![ -~])[\s\S])+"

The pattern matches any one or more (+) chars ([\s\S] matches any whitespace and non-whitespace chars) that does not equal the printable ASCII chars.

Related