[英]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'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> </p><p> </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'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> </p><p> </p>"
let data = /{{\s*\$\w+\s*}}/g
这使:
>>> data
Array(4) [
"{{ $slot }}",
"{{ $example }}",
"{{ $category1 }}",
"{{ $Product2}}"
]
请注意,我在匹配的正则表达式的最后斜杠后添加了g
标志,以返回所有匹配的字符串,而不仅是第一个。
正如其他人所注意到的那样,转义大括号也是一种好习惯,否则当它们包含数字时,您将遇到麻烦,因为这对正则表达式具有特殊含义。
尝试没有第一个斜杠
'{{\\ s * \\ $ \\ w + \\ s *}}'
比赛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'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> </p><p> </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'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> </p><p> </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'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> </p><p> </p>' let data = text.match(new RegExp('{{\\\\s*\\\\$\\\\w+\\\\s*}}', 'g')) console.log(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.