最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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"

发布评论

评论列表(0)

  1. 暂无评论