Regx 查找以单词为标题的字符串,并以 ?/行尾结尾但不包含特定单词



Regx 查找字符串,该字符串以单词为标题并以 ?/行尾结尾,但不包含特定单词

例如,我有以下不同格式的URL,并希望捕获特定部分(页面标识符(

Home: https://www.example.com/course/home#/
courseSummary: https://www.example.com/tag/mypage/course/#/courseSummary?courseName=abc&courceTitle=MyTitle
grounddetails : https://www.example.com/tag/mypage/course/#/options/grounddetails
Certification : https://www.example.com/tag/mypage/course/#/options/Certification/segment
customer: https://www.example.com/tag/mypage/course/#/checkout/customer

但是,每当"确认"一词包含在URL中时,它就不应该匹配。

https://www.example.com/tag/mypage/course/#/**confirmation**?success=true

你能帮忙为它编写正则表达式吗

你可以试试这个:

^w+ *: *http(?:s)://(?!.*confirmation).*(?:?|n)$

正则表达式 101 演示

const regex = /^w+ *: *http(?:s)://(?!.*confirmation).*(?:?|n)$/gm;
const str = `Home: https://www.example.com/course/home#/
courseSummary: https://www.example.com/tag/mypage/course/#/courseSummary?courseName=abc&courceTitle=MyTitle
grounddetails : https://www.example.com/tag/mypage/course/#/options/grounddetails
Certification : https://www.example.com/tag/mypage/course/#/options/Certification/segment
customer: https://www.example.com/tag/mypage/course/#/checkout/customer
But whenever the 'confirmation' word contain in URL then it SHOULD NOT match.
blalba: https://www.example.com/tag/mypage/course/#/**confirmat**?success=true
blalba: https://www.example.com/tag/mypage/course/#/**confirmation**?success=true
blalba: https://www.example.com/tag/mypage/course/#/**confirmatio**?success=true
`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}

// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

最新更新