[英]Strip content from URL and build a string with Javascript and Regex
I would like to strip the item name from a URL using Javascript. 我想使用Javascript从URL中删除项目名称。 Not sure whether Regex is the way to go with this, as I need to capitalize the item name after it is extracted.
不确定正则表达式是否可以解决此问题,因为提取项目名称后需要将其大写。
BEFORE https://www.sportsdirect.com/karrimor-lifestyle-fleece-mens-443326?colcode=44332603
在
https://www.sportsdirect.com/karrimor-lifestyle-fleece-mens-443326?colcode=44332603
之前https://www.sportsdirect.com/karrimor-lifestyle-fleece-mens-443326?colcode=44332603
colcode https://www.sportsdirect.com/karrimor-lifestyle-fleece-mens-443326?colcode=44332603
AFTER "Karrimor Lifestyle Fleece Mens 443226"
之后
"Karrimor Lifestyle Fleece Mens 443226"
So anything between the / after the URL and before the ? 那么在URL的/之后和之间的任何东西? character and stripping out any - characters
字符并删除任何-字符
Use new URL(url).pathname.substring(1)
to get the pathname from your url. 使用
new URL(url).pathname.substring(1)
从您的URL中获取路径名 。 Then split it by -
, map it to uppercase only the first characters of every word and finally join it back. 然后将其除以
-
, 将其仅映射到每个单词的首字母大写 ,最后将其重新组合。 Hope this is what you want :) 希望这就是你想要的:)
var url = 'https://www.sportsdirect.com/karrimor-lifestyle-fleece-mens-443326?colcode=44332603'; var expected_part = new URL(url).pathname.substring(1); // to ignore 1st character var expected_result = expected_part.toLowerCase() .split('-') .map((s) => s.charAt(0).toUpperCase() + s.substring(1)) .join(' '); console.log(expected_result);
This should work: 这应该工作:
var result = 'https://www.sportsdirect.com/karrimor-lifestyle-fleece-mens-443326?colcode=44332603' .replace(/(.*\\/|\\?.*)/g, '') .split(/\\W/) .map(function(w) { return w[0].toUpperCase() + w.substr(1); }) .join(' '); console.log(result);
To give some explanation of what the snippet does (line by line): 为了说明代码段的功能(逐行):
replace(/(.*\\/|\\?.*)/g, '')
remove unwanted parts of URL via RegEx (but keep the path string as it is, with hyphens etc.) replace(/(.*\\/|\\?.*)/g, '')
通过RegEx删除URL的多余部分(但请保留路径字符串的原样,并使用连字符等)。 split(/\\W/)
split into parts along non-word characters (produces array) split(/\\W/)
沿非单词字符分成几部分(产生数组) map(function(w) {...
transform every array element... map(function(w) {...
转换每个数组元素... return w[0].toUpperCase() + w.substr(1);
... to have the first "letter" uppercased. .join(' ');
... and join the elements to a string again, delimited by a whitespace. This is how I did it: 这是我的方法:
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
var input = "https://www.sportsdirect.com/karrimor-lifestyle-fleece-mens-443326?colcode=44332603";
input = input.substring(0, input.indexOf("?"));
input = input.substring(input.lastIndexOf("/") + 1);
input = input.split("-").map(x => capitalizeFirstLetter(x)).join(" ");
capitalizeFirstLetter
was taken from How do I make the first letter of a string uppercase in JavaScript? capitalizeFirstLetter
来自: 如何在JavaScript中使字符串的首字母大写?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.