简体   繁体   English

查找标签之间的特定值

[英]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 @*/我正在尝试编写一个正则表达式,让我从@update行中找到一个值(在打开/*@file和关闭@*/

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在这种情况下,所需的值为2020/03/03 20:44:49

I finished my attempts at我完成了我的尝试

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

And my JS code还有我的 JS 代码

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

Thank you in advance for your help预先感谢您的帮助

You may use this regex with a capture group:您可以将此正则表达式与捕获组一起使用:

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

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • \/\*@file : Match text /*@file \/\*@file : 匹配文本/*@file
  • [^]*? : Match 0 or of any characters (including newlines) (lazy) : 匹配 0 或任何字符(包括换行符)(惰性)
  • @updated[ \t]* : Match text @updated followed by 0 or more space/tabs @updated[ \t]* :匹配文本@updated后跟 0 个或多个空格/制表符
  • (.+) : Match and capture value we want to grab (.+) : 匹配并捕获我们想要获取的值
  • [^]*? : Match 0 or of any characters (including newlines) (lazy) : 匹配 0 或任何字符(包括换行符)(惰性)
  • @\*\/ : Match text @*/ @\*\/ : 匹配文本@*/

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM