如何在javascript中绕过signle引用错误?

时间:2015-06-12 03:03:48

标签: javascript jsp jstl

我需要有一个数组如下,但是(s)之前的单引号会导致错误。 JavaScript不接受它们。有没有办法绕过错误或用空格等其他字符替换单引号?

我尝试使用替换功能但不确定如何使用它。我使用'但它没有用。

 var locations = [
    [
      'Alex's loc', '37.9908372',
      '23.7383394', '0'
    ],
    [
      'John James's loc', '37.9908372',
      '23.7383394', '1'
    ],
    [
      'Norman's loc', '38.075352',
      '23.807885', '3'
    ],
    [
      'Jack Moore's loc', '37.9908372',
      '23.7383394', '2'
    ]
  ];

代码

var locations = [
                <c:forEach var="location" items="${locationes}" varStatus="loop">[
                        '${location.value.name}', '${location.value.latitude}',
                        '${location.value.longitude}', '${loop.index}', </c:forEach> ];

2 个答案:

答案 0 :(得分:1)

你可以用双引号包装单引号,例如:

var locations = [
    [
        "Alex's loc", '37.9908372',
    ]
];

答案 1 :(得分:0)

在无效引用之前使用反斜杠\字符也可以。这称为转义序列

'Alex\'s loc'  // this is represented as the string => Alex's loc

这也是你如何在字符串中获得新的行

'\n' or "\n" for new line.