简体   繁体   English

正则表达式,匹配所有内容,双星号除外

[英]Regex, match everything, except double asterisks

I am creating a text editor interface where the user is able to format the text, and he can see the modifications in a div next to it. 我正在创建一个文本编辑器界面,用户可以在其中设置文本格式,然后他可以在旁边的div中看到修改。

To highlight a bunch of text, I reserved double asterisks as the following ** text **, just like here in stackoverflow. 为了突出显示一堆文本,我在下面的** text **中保留了两个星号,就像在stackoverflow中一样。

My code looks like the following: 我的代码如下所示:

replace(/\*{2}([^\*]*)\*{2}/g, "<strong>$1</strong>")

but my problem is, if the text is including an asterisk, the following output can be seen: 但我的问题是,如果文本中包含星号,则可以看到以下输出:

** text* **, **文字* **,

and not text* 而非文字*

how is it possible to negate double characters for matching to not break the HTML tag in the output? 如何排除用于匹配的双字符以不破坏输出中的HTML标记?

You can deal with single asterisks followed by at least one other character just adding (?:\\*[^*]+)* : 您可以处理单个星号,然后再加上至少一个其他字符,只需添加(?:\\*[^*]+)*

yourstr = yourstr.replace(/\*\*([^*]*(?:\*[^*]+)*)\*\*/g, "<strong>$1</strong>");

Note that escaping * in a character class is useless, and writing \\*{2} is longer than \\*\\* . 请注意,在字符类中转义*是没有用的,写\\*{2}的时间比\\*\\*时间长。

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

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