Regular Expressions - Replace all non letter and numbers except the space

I have the following function.  It replaces all non letters and non numbers.  But, it also replaces the space.  I don't want it to replace the space.  What regular expression would I use?  Thanks in advance


Function ReplaceNonCharAndNonNum(strToReplace,strPattern,strToReplaceWith)
    Dim oRegExp
	Set oRegExp = New RegExp
	oRegExp.Pattern = vPattern
	oRegExp.IgnoreCase = True
	oRegExp.Global = True
	kLeachRegExp = oRegExp.Replace(vStr1, vStr2)
	Set oRegExp = Nothing
End Function
 
vstr = ReplaceNonCharAndNonNum("The quick brown fox jumped over the lazy dog12 *&sd1299 8.", "[^a-z0-9]+", "")

Open in new window

cartch2008Asked:
Who is Participating?
 
CCongdonCommented:
Or if you feel more comfortable keeping something closer to your original:
[^a-z0-9\s]+
0
 
CCongdonCommented:
Try this
 [\W-[ \s]]
It means 'Match any non-word character that is not a whitespace character'
0
 
käµfm³d 👽Commented:
How about:
Function ReplaceNonCharAndNonNum(strToReplace,strPattern,strToReplaceWith)
    Dim oRegExp
    
    Set oRegExp = New RegExp
    oRegExp.Pattern = strPattern
    oRegExp.IgnoreCase = True
    oRegExp.Global = True
    kLeachRegExp = oRegExp.Replace(strToReplace, strToReplaceWith)
    Set oRegExp = Nothing
 
    ReplaceNonCharAndNonNum = kLeachRegExp
End Function
 
vstr = ReplaceNonCharAndNonNum("The quick brown fox jumped over the lazy dog12 *&sd1299 8.", "[^a-z0-9\s]+", "")
 
MsgBox vstr

Open in new window

0
 
cartch2008Author Commented:
This didn't work:   [\W-[ \s]]

But this did:  [^a-z0-9\s]+

thanks!
0
 
CCongdonCommented:
Glad I put the second one in then! :) Not sure why it wouldn't work, but sometimes RegEx implementations differ just enough between platforms that things that defintely work in one place don't always work in another.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.