繁体   English   中英

正则表达式:在标签内抓取文字

[英]Regex: Grab text inside my tag

我正在使用NodeJS读取文件中的文本,但是我对使用正则表达式获取字符串了解不多,所以我正在寻求帮助。

示例文字:

text text {{ 'translate me' | lang }} text {{ 'A' | replace('A', 'B') }} {{"another text"|lang}}

我想在Only {{' Something '| lang}}

产量

['translate me', 'another text']

因此,如何使用Regex进行抓取,谢谢。

哦,它也应该支持一些空格

  • {{'翻译我'| lang}}
  • {{''translate me'| lang}}
  • {{'translate me'| lang}}
  • {{'翻译我'| lang}}

还支持“和”(单引号和双引号)

  • {{“翻译我” | lang}}

两个开口的小卷轴,后跟零个或多个空格,然后是带引号的字符串,然后是零个或多个空格,然后是| lang和两个闭合的curlies,使用后面的(新)外观:

 const text = "text text {{ 'translate me' | lang }} text {{ 'A' | replace('A', 'B') }} {{ 'another text' | lang }}"; console.log(text.match( /(?<=\\{\\{ *')[^']+(?=' *\\| *lang *}})/g )); // or, without lookbehind: const re = /\\{\\{ *'([^']+)(?=' *\\| *lang *}})/g; let match; const matches = []; while ((match = re.exec(text)) !== null) { matches.push(match[1]); } console.log(matches); 

暂无
暂无

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

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