如何过滤字符串数据? [重复]

问题描述 投票:-1回答:2

这个问题在这里已有答案:

我有一个字符串例如: -

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个字符长的值,并删除其余的值。

请帮我实现这个目标。也欢迎替代解决方案。提前致谢。

javascript arrays string servicenow
2个回答
0
投票

假设您想要获取这些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);

-1
投票

像这样:

var arr = ...;
var filtered = arr.filter(word => word.length === 32);

编辑:如果您只想解析GUID,这可能是一个坏主意。当然,像“Ruchika”这样的名字也是32个字符。也许,考虑使用正则表达式。

© www.soinside.com 2019 - 2024. All rights reserved.