K
K
Konstantin Maslennikov2022-03-09 09:49:48
Regular Expressions
Konstantin Maslennikov, 2022-03-09 09:49:48

How to get a given fragment from a string?

Good afternoon! Need help writing a regular expression.

There is this line: test-tes241-text-0123-2-5937-5172473
You need to get this fragment: -5937-5172473 (The format is always the same, just the numbers are different)

ps I need this for a js script, I need to check for the presence data at the end of the line.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vreitech, 2022-03-09
@kosta6832

https://regex101.com/r/cZ1Ijj/1

D
Dr. Bacon, 2022-03-09
@bacon

While it is calculated to what sign, there is a suspicion that regex is not needed here?
UPD "the numbers are different" - if the number is different, then break it into parts by "-" and take the last two, if the number is the same, then generally just take this number from the end of the line.

P
PavelMos, 2022-03-09
@PavelMos

You can do without regex, if you know that you need to take exactly two sequences from the end

let s1 = "test-tes241-text-0123-2-5937-5172473";
let s2=s1.split("-");
console.log('-'+s2[s2.length-1]+'-'+s2[s2.length-2]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question