简体   繁体   English

正则表达式在星号之间匹配字符串

[英]Regex match strings between asterisks

I have a string like this: 我有一个像这样的字符串:

var s = "*string1* *string2* *string3*";

I'm trying to create a regex which returns an array of the three strings between * 我正在尝试创建一个正则表达式,它返回*之间的三个字符串的数组

How can I do that? 我怎样才能做到这一点? I tried using this but I'm not sure if it is correctly escaped 我尝试使用此功能,但不确定是否可以正确转义

s.match("\*(.*)\*")

Thanks 谢谢

You need to use lazy quantifier ? 您需要使用lazy量词? here and also escape \\ 这里也难逃\\

Like this \\*(.*?)\\* Regex101 Demo 像这样\\*(.*?)\\* Regex101演示

Without using lazy quantifier you can do something like this. 不使用lazy量词,您可以执行以下操作。

\\*[^*]+\\* This will match a * everything until a * * . \\*[^*]+\\*这将匹配* everything until a * * everything until a * Regex101 Demo Regex101演示

Use \\\\ instead of \\ in actual code. 在实际代码中使用\\\\代替\\

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

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