Regular expression to use in data table data?

96 views
Skip to first unread message

tobe

unread,
May 30, 2012, 2:07:24 PM5/30/12
to google-visua...@googlegroups.com
Hi everyone!

I am using ASP NET with google chart API.
I am filling a table like this:

var data = google.visualization.arrayToDataTable([
['Movie', 'Status'],
["Movie 1", "On Sale"] ,
["Movie 2", "Sold out"] , 
["Movie 3", "Coming goon"] 
]); 

Data is coming from a SQL Server Database.

The problem is if I have for example a movie which name has single quote (') or double quote (").
When I try to parse that, an error ocurred.

I think I can use regular expressions to parse data without those characters.

Do anyone know which regular expression should I use? 
I am not sure if I know all the characters / scenarios that can "break" my code.

Thanks

asgallant

unread,
May 30, 2012, 2:41:25 PM5/30/12
to google-visua...@googlegroups.com
You'll have to do the regex replace on the server side (or in the SQL query), as the errant quotes will likely cause a syntax error in js and stop execution.  I don't use ASP, so I can't help you with the code to make the regex happen, but the match string you want to use is probably:

(['"])

which will capture both single and double quotes.  I would assume that ASP's regex engine can handle using the captured string in the replacement.

Eric Edens

unread,
May 30, 2012, 2:53:34 PM5/30/12
to google-visua...@googlegroups.com
How about using `replace`?

  JSON.parse(dataTableAsString.replace(/\'/g,"\""))

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-visualization-api/-/OfYKEloZvTkJ.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.

Reply all
Reply to author
Forward
0 new messages