这个问题在这里已有答案:
我有一个字符串例如: -
String: Notification 'Arcosa-Incident assigned to my group' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification's "Groups" field: 'Ruchika Jain' 9efa38ba0ff1310031a1e388b1050e3f
所以基本上我使用.split(' ')
方法将其转换为数组,使其逗号分隔值,现在我想filter
这个数组,只想要32个字符长的值,并删除其余的值。
请帮我实现这个目标。也欢迎替代解决方案。提前致谢。
假设您想要获取这些ID,您只需在字符串上使用带有match
的正则表达式,而无需拆分/过滤它。 (注意:我不得不逃避文中的单引号。)
const str = 'String: Notification \'Arcosa-Incident assigned to my group\' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification\'s "Groups" field: \'Ruchika Jain\' 9efa38ba0ff1310031a1e388b1050e3f';
const matches = str.match(/[a-f0-9]{32}/g);
console.log(matches);
像这样:
var arr = ...;
var filtered = arr.filter(word => word.length === 32);
编辑:如果您只想解析GUID,这可能是一个坏主意。当然,像“Ruchika”这样的名字也是32个字符。也许,考虑使用正则表达式。