how to format javascript array data that contains special characters - Stack Overflow

I'm trying to construct a javascript array from a db query. Each item in the array is a string tha

I'm trying to construct a javascript array from a db query. Each item in the array is a string that can contain many different characters that mess up the array. Single quotes, double quotes, parens, etc...

Here's an example of my current output:

var titleList = new Array('Fallout: New Vegas Teaser-Trailer HD','Saints NFC Champions','Best action scene of all time','NJ Lady ep 5: Our Grandma watches Jersey Shore','Australian Banker Caught Looking At Racy Images Of Model Miranda Kerr On Live Television','LEAKED FOOTAGE: New Griswold's "Vacation" Movie?','"A.D." teaser   (ZOMBIE ANIMATION)'

...and so on

Is there a special way i can encapsulate each array item so that the title's characters dont interfere with the JS?

Thanks for your help.

I'm trying to construct a javascript array from a db query. Each item in the array is a string that can contain many different characters that mess up the array. Single quotes, double quotes, parens, etc...

Here's an example of my current output:

var titleList = new Array('Fallout: New Vegas Teaser-Trailer HD','Saints NFC Champions','Best action scene of all time','NJ Lady ep 5: Our Grandma watches Jersey Shore','Australian Banker Caught Looking At Racy Images Of Model Miranda Kerr On Live Television','LEAKED FOOTAGE: New Griswold's "Vacation" Movie?','"A.D." teaser   (ZOMBIE ANIMATION)'

...and so on

Is there a special way i can encapsulate each array item so that the title's characters dont interfere with the JS?

Thanks for your help.

Share Improve this question asked Feb 8, 2010 at 7:55 CoryCory 5582 gold badges8 silver badges15 bronze badges 4
  • 3 What language do you use to build the array declaration? – Gumbo Commented Feb 8, 2010 at 7:56
  • Does it matter what language is used to build the array when the resulting JS is going to get evaluated in the browser? OP is probably looking for a generic answer than language specific facilities. – Murali VP Commented Feb 8, 2010 at 8:00
  • 1 @Murali VP: Yes, but specific questions lead to specific answers. And maybe the next question is “How can I escape a JavaScript string properly with language X?” – Gumbo Commented Feb 8, 2010 at 8:05
  • I'm using php to query the DB – Cory Commented Feb 9, 2010 at 17:41
Add a ment  | 

3 Answers 3

Reset to default 3

Yes, you can escape these characters. Read about JSON.

Example (Hebrew text in json serialized object):

{"updated_at":"2010/02/01 09:55:15 +0000",
 "title":"\u05d5\u05d9\u05ea\u05d5\u05e8"}

In general you need to escape the quoting characters you’re using for the string declaration. So in case of your string, you need to escape the ' inside your string declaration:

'LEAKED FOOTAGE: New Griswold\'s "Vacation" Movie?'

You either have to do the replacement manually (with some string replace function). Or maybe your language does support JSON functions. That would definitely be the better choice.

You can escape some special characters (double quote, single quote, newline, tab,..) with \ e.g.: var foo = "\" is just a double quote"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745370320a4624767.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信