我需要从下面的字符串中提取:d101461b-a280-431c-b2bf-a3b200c6e575
https://training.shop4support.com/Resources/FileStorage/Products\\d101461b-a280-431c-b2bf-a3b200c6e575/St Stephen\u0027s Neighbourhood
我已经尝试过:
var formattedGuid = guid.match('\\(.*)/').pop();
但是由于“/”等特殊字符,我收到了意外的 token 错误。
最佳答案
尝试匹配 GUID,如下所示:
guid.match(/[0-9a-f]{8}(?:-[0-9a-f]{4}){4}[0-9a-f]{8}/)
所以我缩短了下面的正则表达式来匹配5组十六进制数字(一组可以用[0-9a-f]{length}
匹配):
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}- [0-9a-f]{12}
在初始表达式中我们可以看到一些模式,这些模式也可以分组,所以现在可以描述如下:
[0-9a-f]{8}
- a group of eight numbers
(?:-[0-9a-f]{4}){4}
- four groups of hyphen and four numbers (?:
doesn't save the match)
[0-9a-f]{8}
- a group of eight numbers left
关于javascript - 正则表达式获取字符串中具有特殊字符的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28628594/