如何绕过 javascript 中的单引号错误?

How to bypass signle quote error in javascript?

我需要一个如下所示的数组,但 (s) 之前的单引号会导致错误。 JavaScript 不接受他们。有什么方法可以绕过错误或将单引号替换为 space 等其他字符?

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

 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> ];

你可以用双引号把单引号包起来,比如:

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

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

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

这也是您在字符串中获取乱七八糟的新行的方式

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