如何修复这个正则表达式,以便正确替换*(单词之间)? [英] How to fix this regex so it replaces * properly (between words)?
问题描述
我正在练习正则表达式。我想创建正则表达式,将 *
转换为< em>
,就像Markdown一样:
I'm practicing regex. I thought of creating regex that turn *
into <em>
, just like with Markdown:
el = el.replace(/\*\b/g, '<em>')
el = el.replace(/\b\*|(\.|\,|\?|\!|\*|---|\.\.\.\s)\*/g, '$1</em>')
这在大多数情况下都适用。但是,如果你将正则表达式应用于此,事情会变得混乱:
This works in most cases. However, things gets messy if you apply that regex to this:
Chicken teriy*ai*ki, r*ai*men noodles, spaghetti a la moneg*ai*sque.
它产生这个:
Chicken teriy<em>ai<em>ki, r<em>ai<em>men noodles, spaghetti a la moneg<em>ai<em>sque. And wait for me, often falling asleep.</em></em></em></em></em></em>
如何修改此正则表达式,使其生成如下内容:
How to modify this regex so it produces something like this:
Chicken teriy<em>ai</em>ki, r<em>ai</em>men noodles, spaghetti a la moneg<em>ai</em>sque. And wait for me, often falling asleep.
推荐答案
您可以合并第二个正则表达式中的两个分支两者都以 \ *
模式结束,如(\b | \。|,| \?|!| \ * | - - | \。{3} \s)\ *
(您甚至可以合并 \。|,| \?|!| \ *
单个字符替换为 [。,?!*]
),然后使用
You can merge the two branches in your second regex since both end with \*
pattern, like (\b|\.|,|\?|!|\*|---|\.{3}\s)\*
(you may even merge the \.|,|\?|!|\*
single char alternatives into [.,?!*]
), and then use
var s = "Chicken teriy*ai*ki, r*ai*men noodles, spaghetti a la moneg*ai*sque.";
console.log(
s.replace(/\*\b([^]*?)(\b|[.,?!*]|---|\.{3}\s)\*/g, '<em>$1$2</em>')
)
详细信息
-
\ * \b
- 一个*
后跟一个单词char(字母,数字或_
) -
([^] *?)
- 第1组:任何0+字符,如尽可能少(可以替换为[\\\\ S]
/[\\\\ D]
/[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ c $ c>(\b | [。,?!*] | --- | \。{3} \ s)
- 字边界,。
,,
,?
,!
,*
,---
,...
+空格 -
\ *
-*
char。
\*\b
- a*
that is followed with a word char (letter, digit or_
)([^]*?)
- Group 1: any 0+ chars, as few as possible (may be replaced with a[\s\S]
/[\d\D]
/[\w\W]
if you need more portability), up to the leftmost occurrence of(\b|[.,?!*]|---|\.{3}\s)
- word boundary,.
,,
,?
,!
,*
,---
,...
+ whitespace\*
- a*
char.
这篇关于如何修复这个正则表达式,以便正确替换*(单词之间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!