如何为 javascript 中的特定查询参数解析 URL?
How do I parse a URL for a specific Query Paramter in javascript?
我将有多种 URL,它们都包含相同的查询参数:
https://www.example.com/landing-page/?aid=1234
我想通过在 URL 中搜索 "aid" 查询参数来提取“1234”。
Zapier 中的 javascript 将 运行:
Example javascript block in Zapier
Zapier 注释:我们应该通过设置为名为 inputData 的变量的对象向您的代码提供哪些输入数据(作为字符串)?
我在 javascript 或一般编码方面没有太多经验,但最终结果将是 4 位 "aid" 值,我在通过 webhook 发布到API.
编辑:我检查了类似的答案并感谢链接,但我不确定如何在 Zapier 中使用 "inputData" 和 "url" 提供的答案。
来自 Zapier 平台团队的大卫。
虽然上面的评论将您指向正则表达式,但我推荐一种更原生的方法:实际解析 url。 Node.js 有一个很棒的标准库可以做到这一点:
// the following line is set up in the zapier UI; uncomment if you want to test locally
// const inputData = {url: 'https://www.example.com/landing-page/?aid=1234'}
const url = require('url')
const querystring = require('querystring')
const urlObj = url.parse(inputData.url) /*
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.example.com',
port: null,
hostname: 'www.example.com',
hash: null,
search: '?aid=1234',
query: 'aid=1234',
pathname: '/landing-page/',
path: '/landing-page/?aid=1234',
href: 'https://www.example.com/landing-page/?aid=1234' }
*/
const qsObj = querystring.parse(urlObj.query) // { aid: '1234' }
return { aid: qsObj.aid }
根据您对要查找的数据始终存在的信心,您可能必须在此处进行一些回退,但这将非常可靠地找到您要查找的参数。您还可以在此代码步骤后加上 Filter
,以确保依赖于 aid
的后续步骤不会 运行(如果它丢失了)。
如果您还有其他问题,请告诉我!
我将有多种 URL,它们都包含相同的查询参数:
https://www.example.com/landing-page/?aid=1234
我想通过在 URL 中搜索 "aid" 查询参数来提取“1234”。
Zapier 中的 javascript 将 运行:
Example javascript block in Zapier
Zapier 注释:我们应该通过设置为名为 inputData 的变量的对象向您的代码提供哪些输入数据(作为字符串)?
我在 javascript 或一般编码方面没有太多经验,但最终结果将是 4 位 "aid" 值,我在通过 webhook 发布到API.
编辑:我检查了类似的答案并感谢链接,但我不确定如何在 Zapier 中使用 "inputData" 和 "url" 提供的答案。
来自 Zapier 平台团队的大卫。
虽然上面的评论将您指向正则表达式,但我推荐一种更原生的方法:实际解析 url。 Node.js 有一个很棒的标准库可以做到这一点:
// the following line is set up in the zapier UI; uncomment if you want to test locally
// const inputData = {url: 'https://www.example.com/landing-page/?aid=1234'}
const url = require('url')
const querystring = require('querystring')
const urlObj = url.parse(inputData.url) /*
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.example.com',
port: null,
hostname: 'www.example.com',
hash: null,
search: '?aid=1234',
query: 'aid=1234',
pathname: '/landing-page/',
path: '/landing-page/?aid=1234',
href: 'https://www.example.com/landing-page/?aid=1234' }
*/
const qsObj = querystring.parse(urlObj.query) // { aid: '1234' }
return { aid: qsObj.aid }
根据您对要查找的数据始终存在的信心,您可能必须在此处进行一些回退,但这将非常可靠地找到您要查找的参数。您还可以在此代码步骤后加上 Filter
,以确保依赖于 aid
的后续步骤不会 运行(如果它丢失了)。
如果您还有其他问题,请告诉我!