Return separate characters in the same result

0

Is it possible to get two characters that are in different positions inside the string in the same match (preferably the last one)?

For example:

  

"xxxx a 12 b " - > "ab"

     

"xyxyyxxxxx c 23 d " - > "cd"

I know where the characters are in relation to the end of the string. Using the% regex with% get the two separated. I would like to return them in the same result.

    
asked by anonymous 30.12.2015 / 19:22

1 answer

0

Apparently it is not possible to return on the same result. In my case the solution is to concatenate the matches from the second. That way it works for a regex that returns 1, 2 or more results.

For the example question (in JS):

r = /(.)..(.)$/;
s1 = "xxxxa12b";
m = r.exec(a); // ["a12b", "a", "b"]
s2 = m.slice(1).join(""); // "ab"
    
31.12.2015 / 12:04