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.

Share Improve this question asked Jul 26, 2017 at 18:47 toskvtoskv 31.7k7 gold badges75 silver badges76 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

You 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