从字符串中提取用户名
我有一个字符串名称,可以包括以下可能性,我希望使用Regex表达式以相应的下划线删除字符串的X部分,并仅获取文件名。
字符串的可能性名称:
- XXX_XXX_FILENAME
- XXX_FILENAME
请注意,X是大写,仅由字母{Az}组成。我还能有一个以下的正则表达式,也可以用下划线删除X零件吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以重复1或2次匹配1或更多大写字符[AZ],然后进行下划线。
在替换中使用一个空字符串。
You can repeat 1 or 2 times matching 1 or more uppercase chars [A-Z] followed by an underscore.
In the replacement use an empty string.
Regex demo
您可以尝试以下操作:
“ xxx_xxx_filename” .replace(/^[a-z _]*/g,“”)
如果您知道可以限制Regexp的总字符数:
<代码>“ xxx_xxx_filename” .replace(/^[a-z _] {3,7}/g,“”)
如果您也知道上限的组总数,则可以使用此ALOS:
“ xxx_xxx_filename” .replace(/^([az]+_??){0,2}/g,“”)
You can try this:
"XXX_XXX_filename".replace(/^[A-Z_]*/g,"")
If you know the number of total characters you can restrict the regexp a bit more:
"XXX_XXX_filename".replace(/^[A-Z_]{3,7}/g,"")
If you also know the total number of groups for upper case you could use this alos:
"XXX_XXX_Filename".replace(/^([A-Z]+_?){0,2}/g,"")