繁体   English   中英

将正则表达式匹配到字符串

[英]Match regular expression to string

我有一个文本,想获取数组中的所有匹配项,例如:

[
    ['{{ $slot }}'],
    ['{{$example }}'],
    ['{{ $Product2}}'],
    ['{{$category1 }}']
]

我尝试了以下示例:

 const text = "<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>" let data = text.match('/{{\\s*\\$\\w+\\s*}}') console.log(data) 

如您所见,我得到的结果为null

有什么建议吗?

感谢您的答复!

在JavaScript中,正则表达式文字字符串文字不同,因此match参数必须为/{{\\s*\\$\\w+\\s*}}/而不是'{{\\s*\\$\\w+\\s*}}''/{{\\s*\\$\\w+\\s*}}' 请注意,它周围没有引号。 因此,请尝试:

const text = "<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>"
let data = /{{\s*\$\w+\s*}}/g

这使:

>>> data
Array(4) [
    "{{ $slot }}",
    "{{ $example }}",
    "{{ $category1 }}",
    "{{ $Product2}}"
]

请注意,我在匹配的正则表达式的最后斜杠后添加了g标志,以返回所有匹配的字符串,而不仅是第一个。

正如其他人所注意到的那样,转义大括号也是一种好习惯,否则当它们包含数字时,您将遇到麻烦,因为这对正则表达式具有特殊含义。

尝试没有第一个斜杠

'{{\\ s * \\ $ \\ w + \\ s *}}'

regex101示例

比赛1

全场比赛35-46 {{ $slot }}

比赛2

全场比赛302-315 {{$example }}

比赛3

全场比赛452-467 {{$category1 }}

比赛4

全场比赛589-603 {{ $Product2}}

请尝试以下操作( 请参阅regex101.com ):

const text = "<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>";

let regex = /\{\{\s*(\$[\w_]+)\s*\}\}/g;

let data = text.match(regex);
console.log(data)

只需使用RegExp文字即可

  • 逃避$通过\\$字面上匹配$字符,并且不主张行的结束位置在正则表达式的偶然中间
  • 使用g全局匹配标志来匹配字符串中的每个匹配项

有关可用标志和字符类的更多信息


因此,最终的正则表达式为:

/{{\s*\$\w+\s*}}/g

工作示例:

 const text = '<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>' let data = text.match(/{{\\s*\\$\\w+\\s*}}/g) console.log(data) 

要么

...通过RegExp构造函数 ,只需确保正确转义RegExp字符类,并已经通过前导\\出现了\\字符:

 const text = '<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>' let data = text.match(new RegExp('{{\\\\s*\\\\$\\\\w+\\\\s*}}', 'g')) console.log(data) 

暂无
暂无

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

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