Ron: Edit your code please?

T

The Cleaner

Ron, you posted this some time ago in a forum somewhere.

It lowercases and replaces anything that is not a letter or number with -.

All well and good... however, if there are two of the replaced items together the result is --.

So Ron""Ron becomes ron--ron.

Is it possible to add one last thing to the code below that will replace any multiple of hyphens -- --- ---- and so on with a single hyphen - ?

So ron--ron or ron---ron always becomes ron-ron ? One hyphen allowed only ever.

Thanks in advance!

Option Explicit
Function NonAlphaDash(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "[^a-z0-9]"
NonAlphaDash = re.Replace(LCase(str), "-")
End Function
 
R

Ron Rosenfeld

Ron, you posted this some time ago in a forum somewhere.

It lowercases and replaces anything that is not a letter or number with -.

All well and good... however, if there are two of the replaced items together the result is --.

So Ron""Ron becomes ron--ron.

Is it possible to add one last thing to the code below that will replace any multiple of hyphens -- --- ---- and so on with a single hyphen - ?

So ron--ron or ron---ron always becomes ron-ron ? One hyphen allowed only ever.

Thanks in advance!

Option Explicit
Function NonAlphaDash(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "[^a-z0-9]"
NonAlphaDash = re.Replace(LCase(str), "-")
End Function

Change:

re.Pattern = "[^a-z0-9]+" 'Note the + added to the end of the regex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top