简体   繁体   English

如何使用javascript删除字符串中指定单词之后和句点之前的所有字符?

[英]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.该字符串由不同的位组成,我的代码使用这些位来帮助决定显示图像的位置和时间。

  • 1 (order in group of images) 1(按图像组排序)
  • plex (product name) plex(产品名称)
  • light-blue-striped (colour)浅蓝色条纹(彩色)
  • imagegroup (let's my code know the image is part of a group图像组(让我的代码知道图像是组的一部分

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=15811004321_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.

相关问题 如何在javascript中删除一个单词后的所有字符串? - how to remove all the string after a word in javascript? 使用javascript,如何使字符串只有字母、数字和句点。 删除所有其他字符 - with javascript, how to make string only have letters, numbers, and period. Remove all other characters 如何使用javascript删除破折号前的单词? - How do I remove a word before a dash using javascript? JS - 删除字符串之前/之后的所有字符(并保留该字符串)? - JS - Remove all characters before/after a string (and keep that string)? 如何删除字符串Javascript中的所有字符? - How do i remove all characters in a string Javascript? 使用Javascript删除$ sign后的所有字符 - Remove all the characters after $ sign using Javascript 如何删除指定字符之间的字符串并存储删除的字符串(Javascript) - How to remove string between specified characters and store the removed string (Javascript) 如何使用JavaScript正则表达式匹配包含两个相同单词的字符串中的单词? - How can i match a word in a string which has two same words before it using JavaScript regular expression? 如何使用Javascript在字符串中的每个新行之前/之后插入或删除选项卡? - How can I insert or remove a tab before / after every new line in a string with Javascript? 使用纯JavaScript删除指定ID属性之前的所有元素 - Remove all elements before a specified ID attribute using pure javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM