在指定的str上,querystring.unescape()方法解碼URL percent-encoded個字符。此方法將percent-encoding字符串轉換為普通字符串。這意味著它將通過刪除%符號將任何percent-encoding字符串解碼為普通字符串。此方法遍曆字符串,並在指定的str上需要的地方刪除%編碼。
queryString.unescape()函數由querystring.parse()使用,並且因直接使用而聞名。導入它的主要目的是通過在必要時將queryString.unescape分配給其他函數來使程序代碼具有可變的解碼實現。
用法:
querystring.unescape(str);
參數:此函數僅接受一個參數,如下所述。
- Str:URL percent-encoded個字符將被解碼為普通字符串。
返回值:解碼URL percent-encoded個字符後,它將返回一個正常的字符串。
注意:使用以下命令安裝querystring。
npm i querystring
下麵的示例解釋了查詢string.unescape函數的概念。
範例1:在此示例中,我們正在編碼URL percent-encoded字符串。
Index.js
Javascript
//Importing querystring module
import querystring from "querystring"
// String to be decoded
let str = "I%20love%20geeksforgeeks";
// Using the unescape function to decode
let decodedURL = querystring.unescape(str);
// Printing the decoded url
console.log(decodedURL)
使用以下命令運行index.js文件:
node index.js
輸出:
Decoded string:I love geeksforgeeks
範例2:在該示例中,我們使用querystring.unescape()函數和decodeURIComponent函數對URL percent-encoded字符串進行解碼,並打印兩個函數的輸出。
Javascript
//Importing querystring module
import querystring from "querystring"
// String to be encoded
let str = "I%20love%20geeksforgeeks";
// Using the unescape function to the string
let decodeURL1 = querystring.unescape(str);
let decodeURL2 = decodeURIComponent(str);
// Printing the decoded url
console.log("Decoded string using unescape:" + decodeURL1)
console.log("Decoded string using decodeURIComponent:" + decodeURL2)
if(decodeURL2 === decodeURL1)
console.log("both strings are equal")
輸出:
Decoded string using unescape:I love geeksforgeeks Decoded string using decodeURIComponent:I love geeksforgeeks both strings are equal
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js process.nextTick()用法及代碼示例
- Node.js x509.toLegacyObject()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
注:本文由純淨天空篩選整理自KapilChhipa大神的英文原創作品 Node.js querystring.unescape() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。