admin管理员组文章数量:1430949
Using a regex I would like to find out the prefix and suffix of string like these:
T12231 should match ['T', '12231']
Acw2123 should match ['Acw', '2123']
121Ab should match ['121ab', null]
1213 should match [null, '1213']
Matching only the numbers at the end of the string is easily done with this regex /([0-9]+)$/g
.
Matching everything from the beginning of the string up to this point I did not manage to do. The closest I got was for the 1st group to match everything but the last number with /^(.*)([0-9]+)$/g
.
Using a regex I would like to find out the prefix and suffix of string like these:
T12231 should match ['T', '12231']
Acw2123 should match ['Acw', '2123']
121Ab should match ['121ab', null]
1213 should match [null, '1213']
Matching only the numbers at the end of the string is easily done with this regex /([0-9]+)$/g
.
Matching everything from the beginning of the string up to this point I did not manage to do. The closest I got was for the 1st group to match everything but the last number with /^(.*)([0-9]+)$/g
.
-
2
The first group could be "anything that's not a number" or, from your examples, "any upper/lower case alphabetic character".
\d
is the same as[0-9]
and\D
is the same as[^0-9]
so you could look for something like/^(\D*?)(\d+)$/
... the/g
is pointless if you are using^
and$
together because that will match the whole line. – Stephen P Commented Jul 26, 2017 at 18:53 -
And what does
123Ab345
match ? – user557597 Commented Jul 26, 2017 at 19:06 - @sln ['1234Ab', '345'] – toskv Commented Jul 26, 2017 at 19:11
-
1
Hmm, then it is a situation where the regex needs an assertion to guarantee it does not match the empty string.
^(?=.)(.*)(\d*)$
Nah, I'd say this can't be done ... unless it is forced. – user557597 Commented Jul 26, 2017 at 19:15
3 Answers
Reset to default 3You can make the first capture group lazy, .*?
so it matches as short as possible, i.e, make the second capture group as long as possible:
var s = ["T12231", "Acw2123", "121Ab", "1213"];
console.log(
s.map(x => x.replace(/^(.*?)([0-9]*)$/, "$1 $2"))
);
Push the split result into an array:
var s = ["T12231", "Acw2123", "121Ab", "1213"];
var arr = [];
s.forEach(x => x.replace(/^(.*?)([0-9]*)$/, (string, $1, $2) => arr.push([$1, $2])));
console.log(arr);
You are almost right. Try using this:
var re = /^(.*?)(\d+.*)$/g;
var groups = re.exec(your_string)
Satisfies all cases
^(?=\d|.*\d$)((?:(?!\d+$).)*)(\d*)$
https://regex101./r/BWwsIA/1
^ # BOS
(?= \d | .* \d $ ) # Must begin or end with digit
( # (1 start)
(?: # Cluster begin
(?! \d+ $ ) # Not digits then end
. # Any char
)* # Cluster end, 0 to many times
) # (1 end)
( \d* ) # (2)
$ # EOS
本文标签: javascriptRegex match the prefix and suffix of strings ending with numbersStack Overflow
版权声明:本文标题:javascript - Regex match the prefix and suffix of strings ending with numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745574468a2664244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论