javascript - 正则表达式获取字符串中具有特殊字符的子字符串

标签 javascript jquery

我需要从下面的字符串中提取: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/

相关文章:

javascript - 使用 Node.js 和 Express.js 以及 AngularJs 和 nodemailer 发送电子邮件不发送邮件

javascript - 将图像拖放到 Chrome 中的 contenteditable 到光标处

javascript - 在窗口加载时显示加载图像,直到 DomContentLoaded

javascript - 在 Laravel 4 中使用 jquery Ajax

javascript - 检索完整页面 HTML - jQuery

javascript - fullcalendar:悬停他的 daycell 时显示特定日期的所有事件

javascript - Canvas 从底部中心图像 Angular 旋转?

javascript - 连接正则表达式

javascript - 滚动时淡出/淡入 - 固定值无响应

JavaScript 基本变量返回用户输入的值