正则表达式 - 去除第一个和第二个正斜杠之间的文本 [英] Regex - strip out text between first and second forward slashes
本文介绍了正则表达式 - 去除第一个和第二个正斜杠之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我几乎可以使用这个正则表达式了,但是在使用前导斜杠时遇到了问题 - 谁能看出我哪里出错了?我只想从这个例子中提取第一个字符串projects
"
I've almost got this regex working but am having trouble with the leading forward slash - can anyone see where I'm going wrong with this? I just want to extract the first string "projects
" from this example
/projects/personal/29/56
另见此处 -> http://regexr.com?300av
推荐答案
最简单的方法是使用正斜杠分割字符串
The easiest way is to split string using forward slash
var firstString = url.split('/')[1];
并且您将获得第一个字符串,但是如果您想使用正则表达式提取而不是这样做,请记住不要在正则表达式中添加全局参数.
and you will have first string, but if you want to extract using regext than this will do, just remember don't add global parameter in your regex.
\/([a-zA-Z0-9]{0,})
希望能帮到你
这篇关于正则表达式 - 去除第一个和第二个正斜杠之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文