admin管理员组文章数量:1025207
Take xnum-539 classname random-word xnum-hello hi-239
.
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
.
But that includes the dash when I only want what's after the dash. (ex -539
)
or
rnum-(.*?)\s
which takes the whole thing.
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.
Thanks
Take xnum-539 classname random-word xnum-hello hi-239
.
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
.
But that includes the dash when I only want what's after the dash. (ex -539
)
or
rnum-(.*?)\s
which takes the whole thing.
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.
Thanks
Share Improve this question edited Jan 20, 2017 at 11:05 WillKre asked Jan 20, 2017 at 11:01 WillKreWillKre 6,1686 gold badges36 silver badges64 bronze badges 3-
Is the example the extent of your data, or could there be an arbitrary number of
xnum-
entries? Never mind, Wiktor's answer has you covered :-) – Tim Biegeleisen Commented Jan 20, 2017 at 11:05 -
Actually, there is little one can do here, you just cannot use a plain
String#match
orRegExp#exec
call since JS regex engine does not support a lookbehind. So, choose between slicing or runningRegExp#exec
inside a loop. – Wiktor Stribiżew Commented Jan 20, 2017 at 11:09 -
Unknown number of
xnum-
entries – WillKre Commented Jan 20, 2017 at 11:13
1 Answer
Reset to default 5Note 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.
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);
Pattern details:
xnum-
- a sequence of literal charsxnum-
(\S+)
- Capturing group with ID 1 matching one or more non-whitespace symbols.
Take xnum-539 classname random-word xnum-hello hi-239
.
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
.
But that includes the dash when I only want what's after the dash. (ex -539
)
or
rnum-(.*?)\s
which takes the whole thing.
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.
Thanks
Take xnum-539 classname random-word xnum-hello hi-239
.
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
.
But that includes the dash when I only want what's after the dash. (ex -539
)
or
rnum-(.*?)\s
which takes the whole thing.
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.
Thanks
Share Improve this question edited Jan 20, 2017 at 11:05 WillKre asked Jan 20, 2017 at 11:01 WillKreWillKre 6,1686 gold badges36 silver badges64 bronze badges 3-
Is the example the extent of your data, or could there be an arbitrary number of
xnum-
entries? Never mind, Wiktor's answer has you covered :-) – Tim Biegeleisen Commented Jan 20, 2017 at 11:05 -
Actually, there is little one can do here, you just cannot use a plain
String#match
orRegExp#exec
call since JS regex engine does not support a lookbehind. So, choose between slicing or runningRegExp#exec
inside a loop. – Wiktor Stribiżew Commented Jan 20, 2017 at 11:09 -
Unknown number of
xnum-
entries – WillKre Commented Jan 20, 2017 at 11:13
1 Answer
Reset to default 5Note 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.
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);
Pattern details:
xnum-
- a sequence of literal charsxnum-
(\S+)
- Capturing group with ID 1 matching one or more non-whitespace symbols.
本文标签: javascriptRegex everything after x until spaceStack Overflow
版权声明:本文标题:javascript - Regex everything after x until space - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745586315a2157616.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论