3

I am trying to implement search whole word only in VBScript, I tried appeding characters like space, /, ],) etc. as these characters means end of word. I need to do as many search as the number of characters I want to include using or operator. Is there any way to do it easily in VBScript.

Currently I am doing :-

w_seachString = 
searchString & " " or 
searchString & "/" or 
searchString & "]" or 
searchString & ")" or 
searchString & "}" or 
searchString & "," or 
searchString & "." 

So eventually I am comparing with lots of combination and looking for an effective way to make my variable w_seachString able to search for whole word only.

Kailash Singh
  • 405
  • 2
  • 7
  • 12
  • 1
    You should use Regexp ==> Take a look at this [search and replace WHOLE WORDS ONLY](http://stackoverflow.com/questions/11728717/search-and-replace-whole-words-only/11729848#comment15565679_11729848) – Hackoo Apr 06 '16 at 07:45

1 Answers1

2

Use a regular expression with a word boundary anchor. Demo:

Option Explicit

Function qq(s) : qq = """" & s & """" : End Function

Dim r : Set r = New RegExp
r.Pattern = "\bx\b"
Dim s
For Each s In Split("axb| x |ax|x|\x/|", "|")
    WScript.Echo qq(s), CStr(r.Test(s))
Next

output:

cscript 36443611.vbs
"axb" False
" x " True
"ax" False
"x" True
"\x/" True
"" False
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96