简体   繁体   中英

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.

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.

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. 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) }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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