Rixqi
Rixqi

Reputation: 21

What type of regex would I need to grab from data

What regex would I need to get <t:1583277337:R> to then replace it so it display the proper information (in this case: 3 Years ago)

Why? I'm trying to use it on Discord where Discord automatically converts <t:1583277337:R> to the corresponding data, but the API retrieved data (below) doesnt automatically convert on Discord Embeds

Data

"bio": "𝖓𝖔𝖙 𝖊𝖛𝖊𝖗𝖞𝖔𝖓𝖊 𝖜𝖎𝖑𝖑 𝖇𝖊𝖑𝖎𝖊𝖛𝖊 𝖎𝖓 𝖞𝖔𝖚, 𝖇𝖚𝖙 𝖓𝖔𝖙 𝖊𝖛𝖊𝖗𝖞𝖔𝖓𝖊 𝖒𝖆𝖙𝖙𝖊𝖗𝖘...\n\n\n<t:1583277337:R> <3"

Upvotes: 1

Views: 31

Answers (2)

FoxAlfaBravo
FoxAlfaBravo

Reputation: 106

Example with group name:

<t:(?<timestamp>\d*):R>

Try it here https://regex101.com/

Upvotes: 2

Andrew
Andrew

Reputation: 7555

I won't give you the full solution, but here is the logic:

  1. explicit regex match for an <t:, in sequence
  2. digits of any amount
  3. explicit regex match for :R>

You can use this regex in String#replace. The replace method can accept a callback as its second argument for a more powerful parsing of a match. In this case, you can process the string in the replace callback to whatever text you need

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#parameters

Upvotes: 3

Related Questions