使用正则表达式匹配或拆分来查找特定字符串
我有以下字符串:“user-status status-avail left top-pos
”。
我想查找这个字符串来搜索“status-something”,其中某些东西可以有不同的形式,例如“busy”“not_busy”并返回这个特定的字符串。我应该如何编写我的正则表达式以及我应该使用 split 还是 match 来完成此任务?
我已经尝试过:
str.match(/status-([a-z]+)/);
但我得到:
["status-avail", "avail"]
作为回报,而不仅仅是“status-avail”。
I have the following string : 'user-status status-avail left top-pos
'.
I'd like to lookup this string searching for 'status-something' where something can have different forms like 'busy' 'not_busy' and return this specific string. How should I write my regex and should I use split or match for this task ?
I've tried :
str.match(/status-([a-z]+)/);
but I get :
["status-avail", "avail"]
in return, and not only 'status-avail'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个。您原始帖子中带有此类正则表达式的
某些内容
可能包含字母(忽略大小写)和“_”。statusSubStr
将包含您想要的内容。如果str=user-status status-avail left top-pos
,则statusSubStr=status-avail
。我希望它有帮助。Try this.
something
from you oroginal post with such RegExp could contain letter(with ignored cases) and '_'.statusSubStr
will contain what exactly you want. Ifstr=user-status status-avail left top-pos
thenstatusSubStr=status-avail
. I hope it is helpfull.String.match() 有两种操作模式
字符串
match()
方法有两种使用方式,了解它返回的内容很重要。如果您设置了'g'
global 标志,则该方法将返回一个字符串中所有匹配项的数组,并且该数组的每个元素都包含该字符串的一个完整的整体匹配项。正则表达式。但是,如果您不设置'g'
global 标志,则该方法仅查找字符串中的第一个匹配项,并返回一个包含有关该匹配项的详细信息的数组。在这种情况下,第一个元素 (matches[0]
) 包含整体匹配,后续元素包含正则表达式中捕获组的内容(即matches[1]
包含捕获组 1 的内容,matches[2]
包含捕获组 2 的内容,依此类推)。原始帖子中的正则表达式: (
/status-([az]+)/
) 没有设置'g'
全局标志,因此它返回详细信息关于第一场比赛。由于有一个捕获组,因此返回的数组有两个元素,第一个元素具有整体匹配,第二个元素具有第一个且唯一的捕获组的内容。如果添加全局标志,您将获得字符串中所有完整匹配的数组:
str.match(/status-([az]+)/g);
或者,(如其他人已经指出),您可以从正则表达式中删除捕获括号,在这种情况下,对
match()
方法的调用将返回一个仅包含一个元素的数组,这是总体匹配:str.match(/status-[az]+/);
一旦您了解了这种行为,
String.match()
就会非常有用。String.match() has two modes of operation
The string
match()
method can be used two ways and its important to understand what it returns. If you set the'g'
global flag, then the method returns an array of all the matches in a string and each element of the array contains one whole overall match of the regex. But if you DON'T set the'g'
global flag, then the method only looks for the first match in the string and returns an array containing detailed information about that match. In this case, the first element (matches[0]
) contains the overall match and subsequent elements contain the contents of the capture groups within the regex (i.e.matches[1]
contains the contents of capture group 1,matches[2]
contains the contents of capture group 2, and so on).The regex in the original post: (
/status-([a-z]+)/
) does not have the'g'
global flag set, so it is returning detailed info about the first match. And since there is one capture group, the returned array has two elements, the first element has the overall match and the second has the contents of the first and only capture group.If you add the global flag, you will get an array of all of the whole matches in the string:
str.match(/status-([a-z]+)/g);
Or, (as other abswers have noted), you can remove the capturing parentheses from the regex, in which case the call to the
match()
method will return an array containing only one element, which is the overal match:str.match(/status-[a-z]+/);
the
String.match()
is quite useful once you understand this behavior.从正则表达式中删除括号。 (或者只获取找到的第一个项目)
Remove the parentheses from your regex. (or just get the first item found)