【问题标题】:Regular Expression for String without a "?" character to redirect to string with "?" character没有“?”的字符串的正则表达式用“?”重定向到字符串的字符特点
【发布时间】:2017-05-24 18:53:57
【问题描述】:

在我们的网站上,我们偶尔会遇到动态链接构建不正确的错误。

这样的网址 https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*

应该是这样的:

https://www.test.url.edu/collections/search?edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*

我们想创建一个正则表达式来重定向

/collections/&edan_fq[]=

/collections/search?edan_fq[]=

但是 "edan_fq[]=" 之后的所有内容都可以动态改变——在那之后字符串有数千种排列。

有谁知道这是怎么做的?

【问题讨论】:

标签: regex redirect


【解决方案1】:

如果您在 Regex 中使用 \& 而不使用 Global Flag,它将给出第一个匹配项。我用过JavaScript,请检查一下。

var data = "https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*";

var regex = /\&/
data = data.replace(regex,"search?");
console.log(data);

请查看Regex101中的替换示例。

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多