0
It is possible to obtain in the same match (preferably the last) two characters that are in different positions within the string?
For example:
"xxxxto12b" -> "ab"
"xyxyxyyxxxxxc23d" -> "cd"
I know where the characters are relative to the end of the string. Using regex /(.)..(.)$/
get the two separate. I would like to return them in the same result.
Using only regex I’m not seeing how to capture only parts... you can explain in what context you’ll need it?
– Sergio
@Sergio needs a generic way to extract a substring from a string, depending on the application’s settings. It may be the last character, some in the middle, or in this case, a combination of a character in the middle and end. Using regex would only be possible to change the applied regex, keeping the rest of the execution flow equal.
– mathiasfk
In what language will you treat this string?
– Sergio
@Sergio Javascript
– mathiasfk
Do you think this works for you? -> https://jsfiddle.net/hesLqfoL/
– Sergio
I think the solution is to make a Join of the captured groups, from the second, more or less like you did. So it would work for
/(.)..(.)$/
,/(.)$/
and others. Thank you.– mathiasfk
Put an answer with this idea, it can be useful to others.
– Sergio