1

Lets say I have a comma separated list of extensions:

jpg,jpeg,png

Is there a way that I can turn this into a regex that javascript would smile at? Perhaps a function out there somewhere or some internal way of doing it that I'm completely missing?

So I have a javascript function that I have to actually render to the page from within vb.net which contains an option "allowedFileTypes: " But I need ( for end users purpose ) the ability to convert the above mentioned comma separated string into a valid regex.

Any possible way?

Thank you.

user1447679
  • 3,076
  • 7
  • 32
  • 69
  • Do you want regex for javascript or .NET? (they are not the same) – Matt Wilko Apr 25 '14 at 08:01
  • I need to create a javascript regex from within vb.net code based on that comma separated list of extensions. I'm guessing I need a custom function in vb.net to pull this off... just not sure where to begin. – user1447679 Apr 25 '14 at 08:04
  • What version of .net are you on? It might be easier to call .split() on the list and expose it the List(Of String) as a dynamic jsonarray – rnirnber Apr 25 '14 at 08:09
  • 4.0. In either case if I do that I still need to find a way to turn that array into a regex for javascript. I'm dynamically rendering the javascript function directly from codebehind. – user1447679 Apr 25 '14 at 08:11
  • I just found this: http://stackoverflow.com/a/374956/1447679 I could easily pull something like this off in vb based on a delimited string. Anyone know of any gotcha's that I might face? – user1447679 Apr 25 '14 at 08:16

1 Answers1

0

I would try something like this:

Dim userInput As String = "jpg,jpeg,png"
Dim javascriptRegex As String 

' * Clean up user inputs
javascriptRegex = Regex.replace( userInput, "\s+", "" ) ' Remove white characters
' Add additional automatic corrections and/or checks here if needed...

' * Build Javascript regex
Dim format As String = "/^.*\.(?:{0})$/i"
javascriptRegex = String.Format( format, javascriptRegex.Replace( "," , "|" ) )
' javascriptRegex will contain: /^.*\.(?:jpg|jpeg|png)$/i
Stephan
  • 41,764
  • 65
  • 238
  • 329