正则表达式匹配星号之间的字符串 [英] Regex match strings between asterisks

查看:93
本文介绍了正则表达式匹配星号之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

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("\*(.*)\*")

谢谢

推荐答案

你需要在这里使用 lazy 量词 ? 并转义 \

You need to use lazy quantifier ? here and also escape \

喜欢这个\*(.*?)\* Regex101 Demo一个>

不使用 lazy 量词你可以做这样的事情.

Without using lazy quantifier you can do something like this.

\*[^*]+\* 这将匹配一个 * 直到一个 * *.Regex101 演示

\*[^*]+\* This will match a * everything until a * *. Regex101 Demo

在实际代码中使用 \\ 而不是 \.

Use \\ instead of \ in actual code.

这篇关于正则表达式匹配星号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆