
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用工具模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - querystring.escape() 方法
Node.js querystring 模組提供用於解析和格式化 URL 查詢字串的實用程式。querystring.escape() 方法將從普通字串返回一個百分比編碼的查詢字串。此方法與encodeURIComponent 函式幾乎相同。
此方法透過 % 符號將普通字串編碼為 URL 查詢字串。它將遍歷給定的字串,並在需要的地方新增 % 符號。此方法接受一個引數,即要編碼的字串,並將其編碼後的版本作為輸出返回。
編碼過程將某些字元替換為它們的十六進位制等效項,以使瀏覽器或資料庫不會將其解釋為程式碼的一部分。例如,“&” 在編碼形式中將變為“%26”。
語法
以下是Node.js querystring.escape() 方法的語法:
querystring.escape(str)
引數
str - 此引數儲存一個字串,表示一個編碼的查詢字串。
返回值
此方法返回一個字串,其中包含由給定字串生成的查詢字串。
示例
如果我們將普通字串傳遞給Node.js querystring.escape() 方法,它將返回一個百分比編碼的查詢字串。
在給定的程式中,我們將一個普通字串傳遞給Node.js querystring.escape() 方法。
const querystring = require('node:querystring'); var result = "Articles Node.js Tutorialspoint"; console.log(querystring.escape(result));
輸出
internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'node:querystring' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15) at Function.Module._load (internal/modules/cjs/loader.js:520:25)at Module.require (internal/modules/cjs/loader.js:650:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/home/cg/root/63a038fade650/main.js:1:83) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3)
注意 - 要檢視實際結果,請在本地執行上述程式碼。
正如我們在下面的輸出中看到的,Node.js querystring.escape() 方法返回了一個百分比編碼的查詢字串。
Articles%20Node.js%20Tutorialspoint
示例
如果我們將普通字串傳遞給encodeURIComponent 函式,它將返回一個百分比編碼的查詢字串。
在以下示例中,我們將一個普通字串傳遞給 encodeURIComponent 函式。
const querystring = require('node:querystring'); var encodeURIComponent_func = "Articles HTML Tutorialspoint"; console.log(encodeURIComponent(encodeURIComponent_func))
輸出
internal/modules/cjs/loader.js:596 throw err; ^Error: Cannot find module 'node:querystring' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15) at Function.Module._load (internal/modules/cjs/loader.js:520:25) at Module.require (internal/modules/cjs/loader.js:650:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/home/cg/root/63a038fade650/main.js:1:83) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3)
注意 - 要檢視實際結果,請在本地執行上述程式碼。
正如我們在輸出中看到的,encodeURIComponent 函式將返回一個百分比編碼的查詢字串。
Articles%20HTML%20Tutorialspoint
示例
如果將普通字元串同時傳遞給 Node.js querystring.escape() 方法和 encodeURIComponent() 方法,則返回一個百分比編碼的查詢字串。
在以下示例中,我們嘗試使用 Node.js querystring.escape() 和 encodeURIComponent() 方法對提供的字串進行編碼。
const querystring = require('node:querystring'); let string = "Articles Nodejs Tutorialspoint"; let escaped = querystring.escape(string) let encodedURI = encodeURIComponent(string); console.log("The result of querystring() method: " + escaped); console.log("The result of decodeURIComponent() method: " + encodedURI) if (escaped === encodedURI){ console.log("Both the results are same."); } else{ console.log("Both the results are not same"); }
輸出
internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'node:querystring' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15) at Function.Module._load (internal/modules/cjs/loader.js:520:25) at Module.require (internal/modules/cjs/loader.js:650:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/home/cg/root/63a038fade650/main.js:1:83) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3)
注意 - 要檢視實際結果,請在本地執行上述程式碼。
執行上述程式後,兩種方法都返回了相同的字串。
The result of querystring() method: Articles%20Nodejs%20Tutorialspoint The result of decodeURIComponent() method: Articles%20Nodejs%20Tutorialspoint Both the results are same.