简体   繁体   English

解码JavaScriopt字符串中的unicode转义序列

[英]Decode unicode escape sequences in JavaScriopt string

I have a string like \좋\아\요 . 我有一个像\좋\아\요这样的字符串。

In a node repl (v7.4.0), this statement shows '좋아요' correctly, but in the below code, it's not working. 在节点repl(v7.4.0)中,此语句正确显示了“좋아요”,但是在下面的代码中,它不起作用。

var request = require('request');

request.post(
  {
    url: 'http://book.daum.net/dwr/call/plaincall/ajaxDetailController.getReviewListByPageNo.dwr',
    form: {
      callCount: 1,
      'c0-id': 0,
      'c0-scriptName': 'ajaxDetailController',
      'c0-methodName': 'getReviewListByPageNo',
      'scriptSessionId': '${scriptSessionId}714',
      'batchId': 6,
      'c0-param0': 'Object_Object:{bookid: KOR9791186757093, pageNo: 1, pageSize: 6}'
  }
}, 
(error, response, body) => {
  var str = 's2';
  var regex = new RegExp(str + `.\\w*\\=[\\"\\w\\d\\s\\\\\\&\\:\\/\\.]*\\;`, 'g');
  const arr= body.match(regex);
  /* HERE */
  console.log(arr[14].split('"')[1]);
  console.log(arr[25].split('"')[1]);
  console.log(arr[41].split('"')[1]);
  console.log(arr[35].split('"')[1]);
  console.log(arr[44].split('"')[1]);
  console.log(arr[13].split('"')[1]);
}
);

Why it doesn't show the correct string? 为什么它没有显示正确的字符串?

You probably need to use JSON.parse to escape your string data. 您可能需要使用JSON.parse来转义字符串数据。

Not knowing the structure of your POST response, I'm sure there is a more elegant solution to this problem but this is the best I can do with the information you've provided. 我不知道您的POST响应的结构,我确定有一个更优雅的解决方案,但是对于您所提供的信息,这是我能做到的最好的方法。 Hope this helps. 希望这可以帮助。

 (error, response, body) => { var str = 's2'; var regex = new RegExp(str + `.\\\\w*\\\\=[\\\\"\\\\w\\\\d\\\\s\\\\\\\\\\\\&\\\\:\\\\/\\\\.]*\\\\;`, 'g'); const arr = body.match(regex); /* HERE */ var data = [ arr[14], arr[25], arr[41], arr[35], arr[44], arr[13] ] .map(function (e) { return e.split('"')[1] }) data = JSON.parse('["' + data.join('","') + '"]') console.log(data) }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM