【问题标题】:How to filter a string data? [duplicate]如何过滤字符串数据? [复制]
【发布时间】:2019-03-01 19:50:12
【问题描述】:
我有一个字符串,例如:-
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
【解决方案1】:
假设您想要获取这些 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);
【解决方案2】:
像这样:
var arr = ...;
var filtered = arr.filter(word => word.length === 32);
编辑:如果您只想解析 GUID,这可能不是一个好主意。当然,像“Ruchika”这样的名字也有 32 个字符长。也许,考虑使用正则表达式。