简体   繁体   English

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

[英]Match regular expression to string

I am having a text and would like to get all occurrences in an array, such as: 我有一个文本,想获取数组中的所有匹配项,例如:

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

I tried the following example: 我尝试了以下示例:

 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) 

As you can see I get null as result. 如您所见,我得到的结果为null

Any suggestions why? 有什么建议吗?

I appreciate your replies! 感谢您的答复!

In JavaScript, a regexp literal is different from a string literal , so the argument of match must be /{{\\s*\\$\\w+\\s*}}/ and not '{{\\s*\\$\\w+\\s*}}' or '/{{\\s*\\$\\w+\\s*}}' . 在JavaScript中,正则表达式文字字符串文字不同,因此match参数必须为/{{\\s*\\$\\w+\\s*}}/而不是'{{\\s*\\$\\w+\\s*}}''/{{\\s*\\$\\w+\\s*}}' Notice that there is no quotes around it. 请注意,它周围没有引号。 So try: 因此,请尝试:

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

Which gives: 这使:

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

Note that I added the g flag after the final slash of the regexp for the match to return all the matching strings and not only the first one. 请注意,我在匹配的正则表达式的最后斜杠后添加了g标志,以返回所有匹配的字符串,而不仅是第一个。

As others noticed, it is also a good practice to escape the curly brackets, otherwise you would run into trouble when they will contain a number, as this has a special meaning for regexps. 正如其他人所注意到的那样,转义大括号也是一种好习惯,否则当它们包含数字时,您将遇到麻烦,因为这对正则表达式具有特殊含义。

try without the first slash 尝试没有第一个斜杠

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

regex101 example regex101示例

Match 1 比赛1

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

Match 2 比赛2

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

Match 3 比赛3

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

Match 4 比赛4

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

Please try the following ( see regex101.com ): 请尝试以下操作( 请参阅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)

Just use a RegExp literal and: 只需使用RegExp文字即可

  • escape the $ via \\$ to match literally the $ character and don't assert the end of the line position in the middle of the regular expression by accident 逃避$通过\\$字面上匹配$字符,并且不主张行的结束位置在正则表达式的偶然中间
  • use the g global match flag to match every occurrence in the string 使用g全局匹配标志来匹配字符串中的每个匹配项

More on available flags and character classes 有关可用标志和字符类的更多信息


So the final regex is: 因此,最终的正则表达式为:

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

Working Example: 工作示例:

 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) 

OR 要么

...via RegExp constructor , just make sure to properly escape the RegExp character classes and already present \\ characters via a leading \\ : ...通过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