cartch2008
asked on
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]+", "")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
This didn't work: [\W-[ \s]]
But this did: [^a-z0-9\s]+
thanks!
But this did: [^a-z0-9\s]+
thanks!
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.
[\W-[ \s]]
It means 'Match any non-word character that is not a whitespace character'