[英]How can I remove all characters after a specified word and before a period in a string using javascript?
I have this string:我有这个字符串:
1_plex_light-blue-striped_imagegroup.jpg?v=1581100432
Sometimes it looks like this:有时它看起来像这样:
1_plex_light-blue-striped_imagegroup_d6f347cf-1440-45ef-955d-144ge18d0a00.jpg?v=1581100432
The string is made up of different bits that my code uses to help decide where and when to display the image.该字符串由不同的位组成,我的代码使用这些位来帮助决定显示图像的位置和时间。
Code:代码:
This code is inside a function that is triggered when a colour box is clicked.此代码位于单击颜色框时触发的函数内。 I pass in the colour of the box that was clicked, followed by the word "_imagegroup", and use the string to help be filter the images with the selected colour from the group of images I started out with.我传入被单击的框的颜色,后跟单词“_imagegroup”,并使用该字符串来帮助过滤具有从我开始使用的图像组中选择的颜色的图像。
var imagesArray = makeProductImagesArray();
function filterImageByColourAndGroupType(image) {
return image.masterImagePath.includes(`${colour}_imageset.`);
}
var filteredArray = imagesArray.filter(filterImageByColourAndGroupType);
filteredArray.sort((a, b) => a.masterImagePath.replace(/\D/g,'').localeCompare(b.masterImagePath.replace(/\D/g,'')));
Issue:问题:
Sometimes the match fails because a random string like "d6f347cf-1440-45ef-955d-144ge18d0a00", is added to the image path.有时匹配失败是因为像“d6f347cf-1440-45ef-955d-144ge18d0a00”这样的随机字符串被添加到图像路径中。
How can I ensure that my filter only gives me back images that match this format:我怎样才能确保我的过滤器只给我匹配这种格式的图像:
light-blue-striped_imagegroup.
The filter needs to think the string that is added after "imagegroup" doesn't exist.过滤器需要认为在“imagegroup”之后添加的字符串不存在。 It would see this:它会看到这个:
1_plex_light-blue-striped_imagegroup.jpg?v=1581100432
And to totally ignore:并完全忽略:
_d6f347cf-1440-45ef-955d-144ge18d0a00
Thanks in advance提前致谢
You can use Lookbehind assertion
to match for characters preceded by imagegroup:您可以使用Lookbehind assertion
来匹配图像组前面的字符:
const str = '1_plex_light-blue-striped_imagegroup_d6f347cf-1440-45ef-955d-144ge18d0a00.jpg?v=1581100432'; console.log(str); const regex = "/(?<=imagegroup).*\\./" const newStr = str.replace(regex, '.'); console.log(newStr)
EDIT: Lookbehind assertion
has a form of (?<=y)x
and it matches "x" only if "x" is preceded by "y", to give simple example:编辑: Lookbehind assertion
具有(?<=y)x
的形式,并且仅当“x”以“y”开头时才匹配“x”,举一个简单的例子:
const str = 'matchNOT but matchTHIS'; //this will look for anything containing the word 'match', //but only if it's preceded by the word 'but'; so 'match' before 'but' will be ignored // and 'match' after 'but' will be captured. const newStr = str.match(/(?<=but).*match.*/); console.log(newStr)
You could use a regex.您可以使用正则表达式。 Something like this would work:像这样的事情会起作用:
function filterImageByColourAndGroupType(image) {
const regex = new RegExp(`[0-9]+_.*_${colour}_imagegroup[_.*]?`);
return image.masterImagePath.match(regex) != null;
}
This will match both 1_plex_light-blue-striped_imagegroup.jpg?v=1581100432
and 1_plex_light-blue-striped_imagegroup_d6f347cf-1440-45ef-955d-144ge18d0a00.jpg?v=1581100432
.这将匹配1_plex_light-blue-striped_imagegroup.jpg?v=1581100432
和1_plex_light-blue-striped_imagegroup_d6f347cf-1440-45ef-955d-144ge18d0a00.jpg?v=1581100432
You might need to tweak the exact regex a bit based on the specific constraints of the values (which I don't know and am only guessing at here) but this should get you started.您可能需要根据值的特定约束(我不知道,我只是在这里猜测)稍微调整一下确切的正则表达式,但这应该会让您开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.