G3ck
G3ck

Reputation: 252

Find specific value between tags

I'm trying to write a regular expression to let me find a value from @update line (between opening /*@file and closing @*/

Example code:

/*@file
######################################################
@package        Webapp
@title          your_title
@version        1.0
@tags           tag1,tag2
@lines          215
@created        2020/04/03 20:42:49
@updated        2020/03/03 20:44:49
@description
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
######################################################
@*/

In this case, the desired value is 2020/03/03 20:44:49

I finished my attempts at

[\/][*]@file\n([\S\s]*?)update([\S\s]*?)[@][*][\/]

And my JS code

currentText.replace(/[\/][*]@file\n([\S\s]*?)update([\S\s]*?)[@][*][\/]/g, "SOME TEXT");

Thank you in advance for your help

Upvotes: 0

Views: 42

Answers (2)

Nikita
Nikita

Reputation: 36

I think it could be right

const Alt = currentText.replace(/ /g, '').substring((currentText.replace(/ /g, '').indexOf('@updated')+8),(currentText.replace(/ /g, '').indexOf('@updated')+26))
Alt = Alt.substring(0,10) + ' ' + Alt.substring(11, 18)
console.log(Alt)

Upvotes: 0

anubhava
anubhava

Reputation: 786081

You may use this regex with a capture group:

/\/\*@file[^]*?@updated[ \t]*(.+)[^]*?@\*\//

RegEx Demo

RegEx Details:

  • \/\*@file: Match text /*@file
  • [^]*?: Match 0 or of any characters (including newlines) (lazy)
  • @updated[ \t]*: Match text @updated followed by 0 or more space/tabs
  • (.+): Match and capture value we want to grab
  • [^]*?: Match 0 or of any characters (including newlines) (lazy)
  • @\*\/: Match text @*/

Upvotes: 1

Related Questions