正则表达式x之后的所有内容,直到空格 [英] Regex everything after x until space

查看:43
本文介绍了正则表达式x之后的所有内容,直到空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用 xnum-539类名随机词xnum-hello hi-239 .

我需要挑选出 xnum-之后的所有内容,因此在本例中为 539 hello .

I need to pick out anything that is after xnum-, so in this case the 539, and hello.

我尝试了很多事情,而我要做的最好的事情就是:

I have tried multiple things and the best I've got to is either:

-(.*?)\ s .但这包括破折号,当我只想要破折号后的内容时.(例如 -539 )

-(.*?)\s. But that includes the dash when I only want what's after the dash. (ex -539)

rnum-(.*?)\ s 占用了整个内容.

对于这两种解决方案,我都不需要手动切片.抱歉,如果这是重复的,但我尝试使用其他答案,那么我对正则表达式感到很恐惧.

For both solutions I don't want to have to manually slice. Apologies if this is a duplicate but I've tried to work with other answers and well I'm terrible at regex.

谢谢

推荐答案

请注意,您的正则表达式实际上在比赛结束时需要一个空格( \ s ),因此,停止正则表达式引擎进行匹配您在字符串末尾的值.

Note your regex actually requires a whitespace (\s) at the end of the match, thus, stopping the regex engine to match your value at the end of the string.

您可以使用 /xnum-(\ S +)/g 获得所有匹配项> 正则表达式和访问组1:

You may get all matches with /xnum-(\S+)/g regex and access group 1:

var s = "xnum-539 classname random xnum-hello";
var res=[],m;
var re = /xnum-(\S+)/g;
while ((m=re.exec(s)) !== null) {
  res.push(m[1]);
}
console.log(res);

模式详细信息:

  • xnum--文字字符序列 xnum-
  • (\ S +)-捕获ID为1且与一个或多个非空白符号匹配的组.
  • xnum- - a sequence of literal chars xnum-
  • (\S+) - Capturing group with ID 1 matching one or more non-whitespace symbols.

这篇关于正则表达式x之后的所有内容,直到空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆