简体   繁体   English

query-string pick 方法返回过滤器中的第一个匹配项和损坏的 URL

[英]query-string pick method returns the first match from a filter and corrupted URL

I am trying to clean a URL and only keep some parameters that I need in order to parse them so I was using pick method providing it the url, and the filter which is a regex test method here I am testing to check if the key in the query parameter matches the regular expression我正在尝试清理 URL 并且只保留一些我需要的参数以解析它们所以我使用选择方法为其提供 url 和过滤器,这是一种正则表达式测试方法,我正在测试以检查密钥是否输入查询参数匹配正则表达式

const groupRegex = new RegExp('^(GRP_)[a-zA-Z0-9/-]','g');
export const parseGroups= (url:string)=>{
    let pickedURL = qs.pick(url,(key,value)=>groupRegex.test(key));
    console.log(pickedURL);
}
var url=`http://localhost:3000/tat?GRP_Bob[]=SW&GRP_sa[]=QW&GRP_sa[]=AA&projects[]=MP,PM&releases[]=2021.4,2022.1`
parseGroups(url)

for example http://localhost:3000/tat?GRP_Bob[]=SW&GRP_sa[]=QW&GRP_sa[]=AA&projects[]=MP,PM&releases[]=2021.4,2022.1 it should return http://localhost:3000/tat?GRP_Bob=SW&GRP_sa=QW&GRP_sa=AA yet it only tests for the first request parameter only and logs http://localhost:3000/tat?GRP_Bob%5B%5D=SW例如http://localhost:3000/tat?GRP_Bob[]=SW&GRP_sa[]=QW&GRP_sa[]=AA&projects[]=MP,PM&releases[]=2021.4,2022.1它应该返回http://localhost:3000/tat?GRP_Bob=SW&GRP_sa=QW&GRP_sa=AA但它只测试第一个请求参数并记录http://localhost:3000/tat?GRP_Bob%5B%5D=SW

I am trying to clean the url from any other parameters that doesn't match my regular expression so I can parse the URL and extract the object so it can be like this for example我正在尝试从与我的正则表达式不匹配的任何其他参数中清除 url,因此我可以解析 URL 并提取 ZA8CFDE6331BD59EB2AC96F89111C4B666Z 这样的示例

{
       GRP_Bob:["SW"],
       GRP_sa:["QW","AA"]
 }

Instead of having other parameters parsed also which are not necessary.而不是解析其他不必要的参数。 I know I can just parse the url normally, and then loop on the returned query object, and remove any key that doesn't match the regex, but is there anything wrong I am doing in the above snippet?我知道我可以正常解析 url,然后在返回的query object 上循环,并删除任何与正则表达式不匹配的键,但是我在上面的代码段中做错了什么吗?

UPDATE: I changed the filter function to be (key,value)=>key.startsWith('GRP_'))更新:我将过滤器 function 更改为(key,value)=>key.startsWith('GRP_'))

export const parseGroups= (url:string)=>{
    let pickedURL = qs.pick(url,(key,value)=>key.startsWith('GRP_'));
    console.log(pickedURL);
    let parsedURL = qs.parseUrl(pickedURL)
    console.log(parsedURL.query)
}
var url=`http://localhost:3000/tat?GRP_Bob[]=SW&GRP_sa[]=QW&GRP_sa[]=AA&projects[]=MP,PM&releases[]=2021.4,2022.1`
parseGroups(url)

and the pickedURL logged this http://localhost:3000/tat?GRP_Bob%5B%5D=SW&GRP_sa%5B%5D=QW&GRP_sa%5B%5D=AA which is likely to be correct.并且pickedURL记录了这个http://localhost:3000/tat?GRP_Bob%5B%5D=SW&GRP_sa%5B%5D=QW&GRP_sa%5B%5D=AA这可能是正确的。 it came out like that结果就是这样

GRP_Bob[]: "SW"
GRP_sa[]: (2) ['QW', 'AA']

So I am confused actually what's going on with the regular expression approach, and why the keys in the second approach have [] in it?所以我很困惑实际上正则表达式方法发生了什么,为什么第二种方法中的键有[]

Ah yeh.啊对。 This one is a rare gotcha and totally unexpected every time I see it.这是一个罕见的问题,每次我看到它时都完全出乎意料。 RegExp actually has state. RegExp实际上有 state。 See Why does JavaScript's RegExp maintain state between calls?请参阅为什么 JavaScript 的 RegExp 在调用之间维护 state? and Why does Javascript's regex.exec() not always return the same value?以及为什么 Javascript 的 regex.exec() 并不总是返回相同的值? . .

In your case, you don't need the g flag, so removing that should also fix your problem since that makes the regex stateless.在您的情况下,您不需要g标志,因此删除它也应该可以解决您的问题,因为这会使正则表达式无状态。

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

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